C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Compound Interest Calculator - Java Program

Formula Used

Compund Interest Calculator

Where:

  • P = Principal amount
  • R = Rate of interest (annual)
  • T = Time in years
  • A = Amount after T years
  • CI = Compound Interest = A−PA - PA−P

import java.util.Scanner;

 

public class CompoundInterestCalculator {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

        // Taking inputs

        System.out.print("Enter Principal amount (P): ");

        double principal = sc.nextDouble();

 

        System.out.print("Enter Rate of interest (R): ");

        double rate = sc.nextDouble();

 

        System.out.print("Enter Time in years (T): ");

        double time = sc.nextDouble();

 

        // Calculating compound amount

        double amount = principal * Math.pow((1 + rate / 100), time);

 

        // Compound Interest

        double compoundInterest = amount - principal;

 

        // Display output

        System.out.println("\n----- Compound Interest Details -----");

        System.out.println("Principal       : " + principal);

        System.out.println("Rate (%)        : " + rate);

        System.out.println("Time (years)    : " + time);

        System.out.println("Amount (A)      : " + amount);

        System.out.println("Compound Interest: " + compoundInterest);

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
Enter Principal amount (P): 5000
Enter Rate of interest (R): 5
Enter Time in years (T): 2

----- Compound Interest Details -----
Principal       : 5000.0
Rate (%)        : 5.0
Time (years)    : 2.0
Amount (A)      : 5512.5
Compound Interest: 512.5

Explanation

Step 1 — Take Inputs

  • principal → initial amount
  • rate → yearly interest rate
  • time → how many years

Step 2 — Apply Compound Interest Formula

double amount = principal * Math.pow((1 + rate / 100), time);

  • Math.pow(x, y) computes xyx^yxy
  • Adds interest each year with compounding effect

Step 3 — Compute Compound Interest

double compoundInterest = amount - principal;

CI is total gain after subtracting the original principal.

Step 4 — Display Results

Shows principal, rate, time, final amount, and interest earned.