Compound-interest calculations with for. - Java Language Basics

Java examples for Language Basics:for

Description

Compound-interest calculations with for.

Demo Code

public class Main 
{
   public static void main(String args[])
   {/*  ww w.ja v  a 2 s .  c  om*/
      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);
      } 
   }
}

Result


Related Tutorials