Results 1 to 5 of 5
  1. #1
    subiedude101 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default 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
    
          // 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.

    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 );
    	  }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    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".

  3. #3
    subiedude101 is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    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

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

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

  5. #5
    chessdeviant is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default

    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

Similar Threads

  1. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  2. trouble with loop
    By aamster in forum New To Java
    Replies: 10
    Last Post: 10-16-2009, 09:18 PM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  4. [SOLVED] Trouble with this loop!
    By PureAwesomeness in forum New To Java
    Replies: 35
    Last Post: 02-02-2009, 07:04 PM
  5. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 04:25 AM

Posting Permissions

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