Compound Interest Program in Java

Compound Interest Program in Java | Compound interest is calculated by multiplying the initial principal amount by one plus the annual interest rate raised to the number of compound periods minus one. After that, the total initial amount of the loan is then subtracted from the resulting value.

So, to calculate the annual compound interest, multiply the original amount of your investment or loan, or principal, by the annual interest rate. Add that amount to the principal, then multiply by the interest rate again to get the second year’s compounding interest.

compound interest program in java

For this purpose, the formula of compound interest, including principal sum, is:-

A = P*(1 + r/n)^(n*t)

Where the meaning of these terms is:

A = the future value of the investment/loan, including interest
P = the principal investment amount (the initial deposit or loan amount)
r = the annual interest rate (decimal)
n = the number of times that interest is compounded per unit t
t = the time the money is invested or borrowed for

The above formula gives the total amount. To find the compound interest use,

Compound Interest = A – P

Also see:- Simple interest program in Java

Java program to calculate Compound Interest

Program description:- Write a program to find compound interest by accepting the principle amount, time, and rate values.

To calculate the compound interest we need to find the power. For this purpose, we can use a pre-defined static method pow(double a, double b) of the Math class. It is a static method so to call the pow(-,-) method no need to create an object of the Math class.

import java.util.Scanner;

public class CompoundInterest {
   public static void main(String[] args) {

       // declare variables
       double principal = 0.0, rate = 0.0, time = 0.0;
       int number = 0;
       double totalAmount = 0.0, cinterest = 0.0;

       // create object of Scanner class
       Scanner scan = new Scanner(;

       // read inputs
       System.out.print("Enter principal amount:: ");
       principal = scan.nextDouble();
       System.out.print("Enter the annual compound " + 
                        "interest rate:: ");
       rate = scan.nextDouble();
       rate = rate/100; // convert rate
       System.out.print("Enter time (in years):: ");
       time = scan.nextDouble();
       System.out.print("Enter the number of times that "+
                    +"interest is compounded per year:: ");
       number = scan.nextInt();

       // calculate total amount
       totalAmount = principal * 
                 Math.pow( 1+(rate/number), number*time);

       // calculate compound interest
       cinterest = totalAmount - principal;

       // display results
       System.out.println("Compound interest = "+ cinterest);
       System.out.println("Total amount = "+ totalAmount);

       // close Scanner class object

Sample output for the different test-cases:-

Enter principal amount:: 5000
Enter the annual compound interest rate:: 5
Enter time (in years):: 10
Enter the number of times that interest is compounded per year:: 12
Compound interest = 3235.0474884514006
Total amount = 8235.0474884514

Enter principal amount:: 1500
Enter the annual compound interest rate:: 4.3
Enter time (in years):: 6
Enter the number of times that interest is compounded per year:: 4
Compound interest = 438.83682213410543
Total amount = 1938.8368221341054

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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