Results 1 to 5 of 5
Thread: Trouble with for loop
- 02-12-2011, 07:31 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Trouble with for loop
Modify the compound-interest application of fig5.6 to repeat its steps for interest rates of 5%, 6%, 7%, 8%, 9% and 10%. Use a for loop to vary the interest rate.
here is what fig5.6 looks like:
and here is what I have so far, lol... Obvisouly I have not got very far....It seems so simple but I just can't seem where to begin. Do I need to nest the new for loop within the other for loop? I just need a push in the right direction.Java Code:public class InterestCalc { public static void main( String args[] ) { double amount; // amount on deposit at end of each year double principal = 1000.0; // initial amount before interest double rate = 0.05; // interest rate // display headers System.out.printf( "%s%20s\n", "Year", "Amount on deposit" ); // calculate amount on deposit for each of ten years for ( int year = 1; year <= 10; year++ ) { // calculate new amount for specified year amount = principal * Math.pow( 1.0 + rate, year ); // display the year and the amount System.out.printf( "%4d%,20.2f\n", year, amount ); } // end for }
Java Code:public class InterestCalc { public static void main( String args[] ) { double amount; // amount on deposit at end of each year double principal = 1000.0; // initial amount before interest double rate = 0.05; // interest rate // display headers System.out.printf( "%s%20s\n", "Year", "Amount on deposit" ); // calculate amount on deposit for each of ten years for ( int year = 1; year <= 10; year++ ) { // calculate new amount for specified year amount = principal * Math.pow( 1.0 + rate, year ); // display the year and the amount System.out.printf( "%4d%,20.2f\n", year, amount ); } // end for for (double intRate = .05; intRate <=.1; intRate++ ) { amount = principal * Math.pow( 1.0 + intRate, year ); }
- 02-12-2011, 08:05 PM #2
Yes, they do need to be nested within one another. Basically, on this line here:
amount = principal * Math.pow( 1.0 + rate, year ); (Line 15?)
You want the rate to be varied as in your lower loop. So one loop has to be inside the other one, and instead of "rate", you'll be using the varying "intRate".
- 02-12-2011, 08:42 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
Ok, here is what I did. When I run the program, it doesn't give results of the other interest rates. So I must not be telling the program to increment the interest rate correctly. Am I leaving stuff out of my code?
Java Code:public class InterestCalc { public static void main( String args[] ) { double amount; // amount on deposit at end of each year double principal = 1000.0; // initial amount before interest double rate = 0.05; // interest rate // display headers System.out.printf( "%s%20s\n", "Year", "Amount on deposit" ); // calculate amount on deposit for each of ten years for ( int year = 1; year <= 10; year++ ) { for (double intRate = .05; intRate <=.1; intRate++ ) { amount = principal * Math.pow( 1.0 + intRate, year); // display the year and the amount System.out.printf( "%4d%,20.2f\n", year, amount ); } } // end for } // end main } // end class Interest
- 02-13-2011, 05:53 AM #4
That's because you are only increasing the intRate by 1 (intRate++). You want to increase it by 0.01.
- 02-13-2011, 09:27 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
trouble with loop
By aamster in forum New To JavaReplies: 10Last Post: 10-16-2009, 09:18 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM -
[SOLVED] Trouble with this loop!
By PureAwesomeness in forum New To JavaReplies: 35Last Post: 02-02-2009, 07:04 PM -
Trouble with For loop and variables in a program
By dablyz in forum New To JavaReplies: 12Last Post: 05-06-2008, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks