In this Java example code we will calculate the compound interest with given principal, annual interest rate, unit and time period values.

**Compound Interest = P (1 + R/n) ^{(nt)} – P**

P — the principal investment amount (the initial deposit or loan amount)

r — the annual interest rate (decimal)

n — the number of times that interest is compounded per unit t

t — the time the money is invested or borrowed for

Java method to calculate compound interest with given P, t, r and n values.

1 2 3 4 5 |
public double getCompoundInterest(int p, int t, double r, int n) { double amount = p * Math.pow(1 + (r / n), n * t); double compundInterest = amount - p; return compundInterest; } |