Results 1 to 5 of 5
Thread: Trouble with for loop
 02122011, 07:31 PM #1Member
 Join Date
 Jan 2011
 Posts
 12
 Rep Power
 0
Trouble with for loop
Modify the compoundinterest 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:
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 ); }
 02122011, 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".
 02122011, 08:42 PM #3Member
 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
 02132011, 05:53 AM #4
That's because you are only increasing the intRate by 1 (intRate++). You want to increase it by 0.01.
 02132011, 09:27 PM #5Member
 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: 01312011, 04:21 PM 
trouble with loop
By aamster in forum New To JavaReplies: 10Last Post: 10162009, 09:18 PM 
whileloop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06262009, 08:46 PM 
[SOLVED] Trouble with this loop!
By PureAwesomeness in forum New To JavaReplies: 35Last Post: 02022009, 07:04 PM 
Trouble with For loop and variables in a program
By dablyz in forum New To JavaReplies: 12Last Post: 05062008, 04:25 AM
Bookmarks