# Trouble with for loop

• 02-12-2011, 08:31 PM
subiedude101
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:

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   }```
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.

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, 09:05 PM
Zack
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, 09:42 PM
subiedude101
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?

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, 06:53 AM
Zack
That's because you are only increasing the intRate by 1 (intRate++). You want to increase it by 0.01.
• 02-13-2011, 10:27 PM
chessdeviant
I have the same problem as the original poster I already have the interest rates part done, I am wondering is there a way to put the different rates results in different columns? Thank you