Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# using for loops in calculations

Andrew Albers
Greenhorn
Posts: 10
My problem is the fact that i cannot get the loop to properly run correctly to calculate the second half table which is displayed with Months on the left and CD Value on the right. I just need to be pointed in the right direction, i have spent a lot of time trying to figure this out. thanks for taking the time to look at my code.

Hunter McMillen
Ranch Hand
Posts: 492
are you sure all of the formulas for percent yield, monthly yield are correct?

Andrew Albers
Greenhorn
Posts: 10
the ones before the loop i know are correct, once i figure out how to properly loop the math part ill know if my the equation in the loop is correct. i believe it is though.

Hunter McMillen
Ranch Hand
Posts: 492
So I think I see what you're trying to do here, for the loop part you want to print out the number of months from 1 to (whatever the user enters) right? Then you want to calculate how much money is gained on the cd per month and display it across from the month. is this right??

Andrew Albers
Greenhorn
Posts: 10
yup, then its prints out like:
Months
1
2
3
etc

CD Value
original + 1 month of interest
and so on

except months is across from CD Value.

Hunter McMillen
Ranch Hand
Posts: 492
Ok, I know what you're doing wrong, you have a few problems here.
First this formula:

is wrong.

if my deposit is 500 and my annual % yield is 2% then this returns 500.83333333, this is the total i accumulated not the amount yield per month.
the correct formula is:

Second, you don't need to redefine monthlyYield as monthlyYield1, because it doesnt change from month to month, its constant.

Third, you dont need two for loops, you only need one.
declare a new variable to hold to total cd value;
example:

hope this helps
Hunter.

Andrew Albers
Greenhorn
Posts: 10
thanks alot for the help it is very much appreciated. i gotta say this site is much more beginner friendlier than others.

Piet Souris
Rancher
Posts: 1403
29
Note however that the code above is correct, provided you don't need to work with compound interest, which is very unusual (especially when the number of months is more than 12).

Greetings,
Piet