Let’s learn java program to calculate compound interest.

## Java program to calculate compound interest

To calculate compound interest in java here’s the formula.

P (1 + R/n) ^{(nt)} – P

In compound interest formula,

P – is principal amount.

R – is annual interest rate.

t – is time the money invested or borrowed.

n – number of times interest is compounded. For example if ‘t’ is in years and compounded monthly then ‘n’ value is 12 and if compounded quarterly then ‘n’ value is 4.

For example sachin has ₹ 200000 rupees deposited in ABC bank as fixed deposit. Fixed deposit has an annual interest of 6 percent compounded monthly.

Then compound interest after 6 years is,

P = ₹ 200000

R = 6/100 = 0.06

n = 12

t = 6

Compound interest = 200000 (1 + 0.06 / 12) (12 * 6) – 200000 = ₹ 286408.85

Here’s the compound interest program in java.

public class CompoundInterestDemo { public void calculateCompound(int p, int t, double r, int n) { double number = p * Math.pow(1 + (r / n), n * t); double interest = number - p; System.out.println("Compound interest after " + t + " years: " + interest); System.out.println("Money after " + t + " years: " + number); } public static void main(String[] args) { CompoundInterestDemo obj = new CompoundInterestDemo(); obj.calculateCompound(200000, 6, .06, 12); } }

**Output:**

Compound interest after 6 years: 86408.8556983287

Money after 6 years: 286408.8556983287

Also read – classes and objects in java