-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetirement.java
39 lines (31 loc) · 1.04 KB
/
Retirement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package Retirement;
import java.util.Scanner;
/**
* This program demonstrates a <code>while</code> loop
*
* @author Cay Horstmann
* @version 1.1. 2004-02-10
*/
public class Retirement {
public static void main(String[] args) {
// read inputs
Scanner in = new Scanner(System.in);
System.out.println("How much money do you need to retire?");
double goal = in.nextDouble();
System.out.println("How much money will you contribute every year?");
double payment = in.nextDouble();
System.out.println("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.");
}
}