Wednesday, January 29, 2014

Calculating compound interest over five years.

Source:
public class CompoundInterest {
   public static void main( String args[] ) {

      double principal = 10000;
      int years = 5;
      double rate = .05;

      System.out.printf("Principle: %.2f \n", principal);
      System.out.println("Years: " + years);
      System.out.println("Interest rate: " + rate);

      System.out.println("Total amount componded over the " + years + " years:" );
      for(int x = 1; x <= years; x++) {
         double amount = principal * Math.pow(1+ rate, x);
         System.out.printf("Year " + x + ": %.2f \n" , amount);
      }
   }
}

Output:
   $ java CompoundInterest  
   Principle: 10000.00 
   Years: 5
   Interest rate: 0.05
   Total amount compounded over the 5 years:
   Year 1: 10500.00 
   Year 2: 11025.00 
   Year 3: 11576.25 
   Year 4: 12155.06 
   Year 5: 12762.82 

Blog Archive