Compound Interest

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

IntermediateTopic: Module 1: Basic Java Programs
Back

Java Compound Interest Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
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

Understanding Compound Interest

We use the standard compound interest formula:

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

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents