# Thread: Trouble with for loop

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:

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

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.

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

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 );
}```  Reply With Quote

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".  Reply With Quote

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

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```  Reply With Quote

4. ## That's because you are only increasing the intRate by 1 (intRate++). You want to increase it by 0.01.  Reply With Quote

5. Member Join Date
Feb 2011
Posts
1
Rep Power
0

## 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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•