Java Program to Calculate Compound Interest

In this post, you will see how to write the java program to calculate Compound Interest. Compound interest is the interest on savings calculated on both the initial principal and the accumulated interest from previous periods.

Formula to calculate Compound interest (CI),

Compound Interest (CI) = (P * (1 + r/100)^ n ) – P

Here,
P is the principal amount
r is the rate of interest per annum
n is the number of years the money is invested

For Example:
P= 5000, r= 5, n= 2
CI = (5000 * (1 + 5/100)^2) – 5000 => 512.5

Below is the java program logic for calculating Compound Interest,

package com.javacodepoint.basics;

import java.util.Scanner;

public class CompoundInterest {

	// main method
	public static void main(String[] args) {

		// Declare required variables
		double principal, rate, time, ci;

		// Create scanner object to read user inputs
		Scanner sc = new Scanner(System.in);

		// Read the principal amount
		System.out.print("Enter Principal amount: ");
		principal = sc.nextDouble();

		// Read the rate of interest
		System.out.print("Enter the rate of interest: ");
		rate = sc.nextDouble();

		// Read the number of years
		System.out.print("Enter time in years: ");
		time = sc.nextDouble();

		// Calculate compound interest
		ci = principal * Math.pow((1 + rate / 100), time) - principal;

		// Print the calculated value
		System.out.println("The Compound Interest is:: " + ci);
		System.out.println("The total amount paid (with interest) is:: " + (principal + ci));

	}

}

OUTPUT:

Enter Principal amount: 1000
Enter the rate of interest: 10
Enter time in years: 5
The Compound Interest is:: 610.5100000000004
The total amount paid (with interest) is:: 1610.5100000000004

See also: Java Program to Calculate Simple Interest.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *