close

Program to Find Compound Interest

Last Updated : 19 Jul, 2026

Compound interest is the addition of interest to the principal sum of a loan or deposit, or in other words, interest on interest. It is the result of reinvesting interest, rather than paying it out, so that interest in the next period is then earned on the principal sum plus previously-accumulated interest. 

Formula: The amount accumulated after T years when interest is compounded annually is:
Amount = P × (1 + R / 100)^T
Compound Interest = Amount − P
Where:
P = Principal amount
R = Annual rate of interest (in percentage)
T = Time period (in years)

Example: 

Input: P = 1200, R = 5.4, T = 2
Output: 133.0992
Explanation:
Using the compound interest formula:
Amount = P × (1 + R / 100)^T = 1200 × (1 + 5.4 / 100)^2 = 1200 × (1.054)^2
Amount ≈ 1333.0992
Compound Interest = Amount − P = 1333.0992 − 1200
Compound Interest ≈ 133.09922

C++
#include <iostream>
#include <cmath>
using namespace std;

double compoundInterest(double principal, double rate, double time) {

    // Calculate the final amount after compound interest
    double amount = principal * pow(1 + rate / 100, time);

    // Compound Interest = Amount - Principal
    double compoundInterest = amount - principal;

    return compoundInterest;
}

int main() {
    double principal = 1200;
    double rate = 5.4;
    double time = 2;

    cout << "Compound Interest = "
         << compoundInterest(principal, rate, time);

    return 0;
}
C
#include <stdio.h>
#include <math.h>

double compoundInterest(double principal, double rate, double time) {

    // Calculate the final amount after compound interest
    double amount = principal * pow(1 + rate / 100, time);

    // Compound Interest = Amount - Principal
    double compoundInterest = amount - principal;

    return compoundInterest;
}

int main() {
    double principal = 1200;
    double rate = 5.4;
    double time = 2;

    printf("Compound Interest = %.2lf", compoundInterest(principal, rate, time));

    return 0;
}
Java
import java.lang.Math;

public class GFG {
    public static double compoundInterest(double principal, double rate, double time) {

        // Calculate the final amount after compound interest
        double amount = principal * Math.pow(1 + rate / 100, time);

        // Compound Interest = Amount - Principal
        double compoundInterest = amount - principal;

        return compoundInterest;
    }

    public static void main(String[] args) {
        double principal = 1200;
        double rate = 5.4;
        double time = 2;

        System.out.println("Compound Interest = " + compoundInterest(principal, rate, time));
    }
}
Python
import math

def compoundInterest(principal, rate, time):

    # Calculate the final amount after compound interest
    amount = principal * math.pow(1 + rate / 100, time)

    # Compound Interest = Amount - Principal
    compoundInterest = amount - principal

    return compoundInterest

if __name__ == "__main__":
    principal = 1200
    rate = 5.4
    time = 2
    
    print("Compound Interest =", compoundInterest(principal, rate, time))
C#
using System;

class GFG {
    static double compoundInterest(double principal, double rate, double time) {

        // Calculate the final amount after compound interest
        double amount = principal * Math.Pow(1 + rate / 100, time);

        // Compound Interest = Amount - Principal
        double compoundInterest = amount - principal;

        return compoundInterest;
    }

    static void Main() {
        double principal = 1200;
        double rate = 5.4;
        double time = 2;

        Console.WriteLine("Compound Interest = " + compoundInterest(principal, rate, time));
    }
}
JavaScript
function compoundInterest(principal, rate, time) {

    // Calculate the final amount after compound interest
    let amount = principal * Math.pow(1 + rate / 100, time);

    // Compound Interest = Amount - Principal
    let compoundInterest = amount - principal;

    return compoundInterest;
}

// Driver code
let principal = 1200;
let rate = 5.4;
let time = 2;

console.log("Compound Interest =", compoundInterest(principal, rate, time));

Output
Compound Interest = 133.099
Comment