Java Program to Calculate the Compound Interest

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to calculate the Compound interest. Compound Interest is calculated using the following formula −

Principle*(1+(rate / 100))^time – Principle

Compound Interest − The percentage interest charged on principal and accrued interest. Rates are higher compared to Simple Interest.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter a Principle number : 100000
Enter Interest rate : 5
Enter a Time period in years : 3

Output

The desired output would be −

The Compound Interest is : 15762.50000000001

Algorithm

Step 1 – START
Step 2 – Declare four float values principle, rate, time, compound_interest
Step 3 – Read values of principle, rate, time, from the user
Step 4 – Perform “principle * (Math.pow((1 + rate / 100), time)) – principle” to calculate the
compound interest and store it in a simple_interest variable
Step 8 – Display compound_interest
Step 10 – STOP

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class CompoundInterest {
   public static void main (String args[]){
      double principle, rate, time, compound_interest;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A Scanner object has been defined ");
      System.out.print("Enter a Principle number : ");
      principle = my_scanner.nextInt();
      System.out.print("Enter Interest rate : ");
      rate = my_scanner.nextInt();
      System.out.print("Enter a Time period in years : ");
      time = my_scanner.nextInt();
      compound_interest = principle * (Math.pow((1 + rate / 100), time)) - principle;
      System.out.println("The Compound Interest is : " + compound_interest);
   }
}

Output

Required packages have been imported
A Scanner object has been defined
Enter a Principle number : 100000
Enter Interest rate : 5
Enter a Time period in years : 3
The Compound Interest is : 15762.500000000015

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class CompoundInterest{
   public static void main (String args[]){
      double principle, rate, time, compound_interest;
      principle = 100000;
      rate = 5;
      time = 3;
      System.out.printf("The Principle amount is %f \nThe interest rate is %f \nThe time period in years is %f " , principle, rate, time);
      compound_interest = principle * (Math.pow((1 + rate / 100), time)) - principle;
      System.out.println("\nThe Compound Interest is: " + compound_interest);
   }
}

Output

The Principle amount is 100000.000000
The interest rate is 5.000000
The time period in years is 3.000000
The Compound Interest is: 15762.50000000001
raja
Updated on 21-Feb-2022 10:41:15

Advertisements