-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureValueApp.java
More file actions
46 lines (39 loc) · 1.7 KB
/
FutureValueApp.java
File metadata and controls
46 lines (39 loc) · 1.7 KB
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
40
41
42
43
44
45
46
import java.util.Scanner;
import java.text.NumberFormat;
public class FutureValueApp {
public static void main(String[] args) {
System.out.println("The Future Value Calculator\n");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// get the input from the user
System.out.print("Enter monthly investment: ");
double monthlyInvestment = sc.nextDouble();
System.out.print("Enter yearly interest rate: ");
double interestRate = sc.nextDouble();
System.out.print("Enter number of years: ");
int years = sc.nextInt();
// convert yearly values to monthly values
double monthlyInterestRate = interestRate / 12 / 100;
int months = years * 12;
// use a for loop to calculate the future value
double futureValue = 0.0;
for (int i = 1; i <= months; i++) {
futureValue = (futureValue + monthlyInvestment) *
(1 + monthlyInterestRate);
System.out.println("Month " + i + ": " + futureValue);
}
// format the result and display it to the user
NumberFormat currency = NumberFormat.getCurrencyInstance();
System.out.println("Future value: "
+ currency.format(futureValue));
System.out.println();
// see if the user wants to continue
System.out.print("Continue? (y/n): ");
choice = sc.next();
System.out.println();
}
System.out.println("Bye!");
}
}