Compound Interest In Java
Chapter:
Math Class
Last Updated:
04-05-2016 18:30:49 UTC
Program:
/* ............... START ............... */
public class JavaCompoundInterest {
public static void main(String args[]) {
double principal = 100000;
int years = 4;
double rate = .06;
System.out.printf("Principle: %.2f \n", principal);
System.out.println("Years: " + years);
System.out.println("Interest rate: " + rate);
System.out.println("Total amount componded over the " + years + " years:");
for (int x = 1; x <= years; x++) {
double amount = principal * Math.pow(1 + rate, x);
System.out.printf("Year " + x + ": %.2f \n", amount);
}
}
}
/* ............... END ............... */
Output
Principle: 100000.00
Years: 4
Interest rate: 0.06
Total amount componded over the 4 years:
Year 1: 106000.00
Year 2: 112360.00
Year 3: 119101.60
Year 4: 126247.70
Notes:
-
Compound interest can be calculated with the following formula:
- A = amount
- P = principal
- R = rate
- n = years
-
- A=P(1+R)^n
Tags
Compound Interest, Java, Math