**Calculate Compound Interest in Java**

Here you will get the example code to **Calculate Compound Interest** in Java programming.

**Calculate Compound Interest Formula**

**Amt = P(1 + r/n)^(nt)**

Where:

Amt is final amount(principal + interest).

P is the principal amount

r is rate of interest (yearly).

n is the annual number of times interest is compounded.

t is time in years.

**Example:**

Suppose you invest rupees 1,000 (P) in a savings account with an annual interest rate of 5% (r). The interest is compounded annually (n = 1), and you plan to leave the money in the account for 3 years (t).

Using the compound interest formula:

Amt = P(1 + r/n)^(nt)

Amt = 1,000(1 + 0.05/1)^(1*3)

Amt = 1,000(1 + 0.05)^3

Amt = 1,000(1.05)^3

Amt = 1,000(1.157625)

Amt = 1,157.63

So, after 3 years of investing rupees 1,000 in a savings account with a 5% annual interest rate. You would have approximately rupees 1,157.63 in your account.

## Program to Calculate Compound Interest in Java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //Calculate Compound Interest in Java import java.util.Scanner; public class CIcalc { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter principal amount: "); double p = scanner.nextDouble(); System.out.print("Enter annual interest rate (in decimal form): "); double r = scanner.nextDouble(); System.out.print("Enter the number of times interest is compounded per year: "); int cf = scanner.nextInt(); System.out.print("Enter number of years: "); int t = scanner.nextInt(); scanner.close(); // Calculate compound interest double amt = p * Math.pow(1 + (r / cf), cf * t); // Display the result System.out.println("The final amount after " + t + " years will be: " + amt); } } |

**Output**

*C:\CodeRevise\java>javac CIcalc.java*

*C:\CodeRevise\java>java CIcalc*

*Enter principal amount: 1000*

*Enter annual interest rate (in decimal form): 0.05*

*Enter the number of times interest is compounded per year: 1*

*Enter number of years: 3*

*The final amount after 3 years will be: 1157.6250000000002*

Check out our other Java Programming Examples