4 Replies - 2246 Views - Last Post: 30 October 2010 - 06:02 PM Rate Topic: -----

#1 aroq19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-October 10

Java variable interest rate using for loop.

Posted 30 October 2010 - 02:48 PM

Hello everyone,

I'm having trouble repeating the code for different a set of years for each different percentage rate. I'm not sure what I'm doing wrong. I'm very new at this and I'm sorry if this is very simple question. Please let me know if you need more information from me.

( This is the code I'm using. )

public class Interest
{
   public static void main( String [] args )
   {
   
      double amount; // amount on deposit at the end of each year
      double principal = 1000.0; // initial amount before interest

      // 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 rate = .05; rate <=.10; rate ++ )
         {
      {
         // calculate new amount for each 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
      } // end for
   } // end main
} // end class Interest



( This is the result I'm getting for just the interest rate of 5%. I'm looking to show a different set of years for each percentage rate from 5%-10%. )

C:\Users\Anthony\Documents\CSC 130>java Interest
Year Amount on deposit
1 1,050.00
2 1,102.50
3 1,157.63
4 1,215.51
5 1,276.28
6 1,340.10
7 1,407.10
8 1,477.46
9 1,551.33
10 1,628.89

Is This A Good Question/Topic? 0
  • +

Replies To: Java variable interest rate using for loop.

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Java variable interest rate using for loop.

Posted 30 October 2010 - 02:58 PM

You will have to play with your formatting but your problem is that your 2nd For loop is incrementing by 1 per loop...but your maximum value is .10...you need to increment by .01 or .025, etc
         for ( double rate = .05; rate <=.10; rate +=.01 )


This post has been edited by bcranger: 30 October 2010 - 02:59 PM

Was This Post Helpful? 1
  • +
  • -

#3 aroq19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-October 10

Re: Java variable interest rate using for loop.

Posted 30 October 2010 - 03:03 PM

View Postbcranger, on 30 October 2010 - 01:58 PM, said:

You will have to play with your formatting but your problem is that your 2nd For loop is incrementing by 1 per loop...but your maximum value is .10...you need to increment by .01 or .025, etc
         for ( double rate = .05; rate <=.10; rate +=.01 )



Wow. Thanks. I'm a lot better off now. Now, I just need to figure out how to organize all the different sets of years.
Was This Post Helpful? 0
  • +
  • -

#4 aroq19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-October 10

Re: Java variable interest rate using for loop.

Posted 30 October 2010 - 04:23 PM

Any suggestions?

I'm trying to get it to look like
year                           except i'm showing
                               year

1                              1 
2                              1   
3                              1 
4                              1 
5                              1 
6                              2
7                              2
8                              2
9                              2
10                             2
                              
1                              3
2                              3
3                              3   
4                              3
5                              3  
6                              4       
7                              4     
8                              4             
9                              4
10                             4             

etc..

Was This Post Helpful? 0
  • +
  • -

#5 aroq19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-October 10

Re: Java variable interest rate using for loop.

Posted 30 October 2010 - 06:02 PM

Nevermind I figured it out. I had to switch around my "for" statements. Thanks for everyone that helped!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1