Compound Interest

Calculate compound interest using principal, rate, time, and compounding frequency.

JavaIntermediate
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter principal: ");
        double p = sc.nextDouble();
        System.out.print("Enter annual rate of interest: ");
        double r = sc.nextDouble();
        System.out.print("Enter time (in years): ");
        double t = sc.nextDouble();
        System.out.print("Enter number of times interest applied per year: ");
        int n = sc.nextInt();

        double amount = p * Math.pow(1 + (r / (100 * n)), n * t);
        double ci = amount - p;

        System.out.println("Compound Amount = " + amount);
        System.out.println("Compound Interest = " + ci);

        sc.close();
    }
}

Output

Enter principal: 1000
Enter annual rate of interest: 5
Enter time (in years): 2
Enter number of times interest applied per year: 1
Compound Amount = 1102.5
Compound Interest = 102.5

We use the standard compound interest formula:

A = P (1 + r/(100n))^(nt) and CI = A - P.