Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Calculate Compound Interest

In this tutorial, we will learn how to find the compound interest when the principal, rate of interest, time period, and the number of times the interest is compounded are given. But before moving further, if you are not familiar with the concept of the arithmetic operator in java, then do check the article on Operators in Java.

Input: Enter the principal amount: 6200.0

Enter the rate: 11.0

Enter the time period: 2.0

Output:

Compound Interest: 886600.0

The amount at the end of 2 years: 892800.0

The above problem can be solved in the following ways:

Approach 1: When the values are user-defined

Approach 2: When the values are pre-defined

Let us look at each of these approaches separately.

Program 1: To Calculate the Compound Interest

In this program, we will see how to find the compound interest using the formula when the values are user-defined. This means, first we will first ask the user to initialize the variables, and then we will find the compound interest using the formula.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class to take the input from the user.
  3. Declare variables for the principal amount, rate of interest, time period, and the number of times the interest is compounded.
  4. Ask the user to initialize these variables.
  5. Calculate the compound interest using the formula.
  6. Print the value of compound interest.
  7. Print the amount after compound interest.
  8. Stop

Below is the code for the same.

//Java Program to calculate the compound interest
import java.util.*;
public class Main
{
    public static void main(String args[]) 
    {
        //Take input from the user
        //Create an instance of the Scanner class
    	Scanner sc = new Scanner(System.in);
        //Declare variables
        float p, r, t, n;
        System.out.println("Enter the Principal : ");
        p = sc.nextFloat();     //Initialize the variables
        System.out.println("Enter the Rate of interest : ");
        r = sc.nextFloat();     //Initialize the variables
        System.out.println("Enter the Time period : ");
        t = sc.nextFloat();     //Initialize the variables
        System.out.println("Enter the number of times that interest is compounded per unit t");
        n=sc.nextFloat();      //Initialize the variables
        sc.close();
        
        //Calculate the compound interest
    	double amount = p * Math.pow(1 + (r / n), n * t);
        double cinterest = amount - p;
        System.out.println("Compound Interest after " + t + " years: "+cinterest);
        System.out.println("Amount after " + t + " years: "+amount);
    }
}


Enter the Principal amount: 5200
Enter the Rate of interest: 12
Enter the Time period: 3
Enter the number of times that interest is compounded per unit t: 2
Compound Interest after 3.0 years: 6.117696E8
Amount after 3.0 years: 6.117748E8

Program 2: To Calculate the Compound Interest

In this program, we will see how to find the compound interest using the formula when the values are pre-defined in the program.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class to take the input from the user.
  3. Declare variables for the principal amount, rate of interest, time period, and the number of times the interest is compounded.
  4. Initialize these variables.
  5. Calculate the compound interest using the formula.
  6. Print the value of compound interest.
  7. Print the amount after compound interest.
  8. Stop

Below is the code for the same.

//Java Program to calculate the compound interest
public class Main
{
    public static void main(String args[]) 
    {
        //Declare and initialize the variables 
        float p = 4500, r = 10, t = 2 , n=1; 
        //Print the variables and their corresponding values
        System.out.println("The entered principle amount is = " + p);
        System.out.println("The entered rate is = " + r);
        System.out.println("The entered time period is " + t);
        System.out.println("The entered number of times the interest is compounded is " + n);
        
        //Calculate the compound interest and the amount
    	double amount = p * Math.pow(1 + (r / n), n * t);
        double cinterest = amount - p;
        System.out.println("Compound Interest after " + t + " years: "+cinterest);
        System.out.println("Amount after " + t + " years: "+amount);
    }
}


The entered principle amount is = 4500.0
The entered rate is = 10.0
The entered time period is 2.0
The entered number of times the interest is compounded is 1.0
Compound Interest after 2.0 years: 540000.0
Amount after 2.0 years: 544500.0

Program 3: To Find the Compound Interest

In this program, we will see how to find the compound interest using the formula when the values are user-defined. This means, first we will first ask the user to initialize the variables, and then a user-defined method to calculate the compound interest.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class to take the input from the user.
  3. Declare variables for the principal amount, rate of interest, time period, and the number of times the interest is compounded.
  4. Ask the user to initialize these variables.
  5. Call a method to calculate the compound interest.
  6. Calculate the compound interest using the formula.
  7. Print the value of compound interest.
  8. Print the amount after compound interest.
  9. Stop

Below is the code for the same.

//Java Program to calculate the compound interest
public class Main
{
    public static void main(String args[]) 
    {
        //Declare and initialize the variables 
        float p = 2900, r = 18, t = 2 , n=1; 
        //Print the variables and their corresponding values
        System.out.println("The entered principle amount is = " + p);
        System.out.println("The entered rate is = " + r);
        System.out.println("The entered time period is " + t);
        System.out.println("The entered number of times the interest is compounded is " + n);
        findCi(p,r,t,n);
    }
    public static void findCi(float p, float r, float t, float n)
    {
        //Calculate the compound interest and the amount
    	double amount = p * Math.pow(1 + (r / n), n * t);
        double cinterest = amount - p;
        System.out.println("Compound Interest after " + t + " years: "+cinterest);
        System.out.println("Amount after " + t + " years: "+amount);
    }
}


The entered principle amount is = 2900.0
The entered rate is = 18.0
The entered time period is 2.0
The entered number of times the interest is compounded is 1.0
Compound Interest after 2.0 years: 1044000.0
Amount after 2.0 years: 1046900.0