Retirement.java


import java.util.*;

/**
 * This program demonstrates a while loop.
 * @version 1.20 2004-02-10
 * @author Cay Horstmann
 */
public class Retirement
{
   public static void main(String[] args)
   {
      // read inputs
      Scanner in = new Scanner(System.in);

      System.out.print("How much money do you need to retire? ");
      double goal = in.nextDouble();

      System.out.print("How much money will you contribute every year? ");
      double payment = in.nextDouble();

      System.out.print("Interest rate in %: ");
      double interestRate = in.nextDouble();

      double balance = 0;
      int years = 0;

      // update account balance while goal isn't reached
      while (balance < goal)
      {
         // add this year's payment and interest
         balance += payment;
         double interest = balance * interestRate / 100;
         balance += interest;
         years++;
      }

      System.out.println("You can retire in " + years + " years.");
   }
}


Results

C:\ece595_06\day04\Retirement>javac Retirement.java

C:\ece595_06\day04\Retirement>java Retirement
How much money do you need to retire? 500000
How much money will you contribute every year? 5000
Interest rate in %: 4
You can retire in 41 years.

Reference

Cay S. Horstmann and Gary Cornell Core Java, Volume I – Fundamentals (10th Edition)
Prentice Hall, 2015. chapter 3


Maintained by John Loomis, updated Mon Sep 02 11:41:04 2013