-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompInterestCalc.java
More file actions
47 lines (35 loc) · 1.55 KB
/
CompInterestCalc.java
File metadata and controls
47 lines (35 loc) · 1.55 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
47
import java.util.Scanner;
public class CompInterestCalc {
public static void main(String[] args){
// My attempted Compound Interest Calculator project.
/*
A = P [ 1 + r/n ]^nt
A is amount, P is principal amount
r is Interest Rate in decimal or percentage
n is the Number of times interest is compounded per year
t is Time in years
*/
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the principal amount: $");
double principal = scanner.nextDouble();
System.out.print("Interest Rate (in %): ");
double interestRate = scanner.nextDouble();
interestRate /= 100;
// interest rate will be divided by 100, to give in % form.
System.out.print("How many times compounded per year: ");
int timesCompounded = scanner.nextInt();
System.out.print("Number of years: ");
int years = scanner.nextInt();
/*
double Amount = 1 + interestRate / timesCompounded;
//nt is timesCompounded(n) x Years(t)
double nt = timesCompounded * years;
double Amount2 = Math.pow(Amount, nt);
double finalAmount = Amount2 * principal;
*/
// Shorter version, the using nested brackets.
double finalAmount = principal * Math.pow((1 + interestRate / timesCompounded), timesCompounded * years);
System.out.printf("\nThe final amount after %d years is $%.2f", years, finalAmount);
scanner.close();
}
}