Java Program to calculate Simple Interest and Compound Interest

JavaObject Oriented ProgrammingProgramming

In this article, we will input the Principal Amount, Rate and Time (Years) from the user to find the Simple Interest and Compound Interest.

Simple Interest − The percentage interest on total principal amount. Returns are less compared to Compound Interest.

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

Input

Let's say the following is our input −

Principal = 25000.0
Annual Rate of Interest = 10.0
Time (years) = 4.0

Output

Our output should be the following calculating Simple and Compound Interest −

Simple Interest: 10000.0
Compound Interest: 11602.500000000007

Algorithm

Step 1 – START
Step 2 – Declare 5 integers p, r, t, s_interest, c_interest
Step 3 – Read values of p, r, t, from user
Step 4 – Perform (p * r * t)/100
Step 5 – Perform p * Math.pow(1.0+r/100.0,t) - p;
Step 6 – Store the output of Step 4 in s_interest
Step 7 – Store the output of Step 5 in c_interest
Step 8 – Display s_interest
Step 9 – Display c_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.*;
   public class SimpleAndCompountInterest{
   public static void main(String []args){
      double p, r, t, s_interest, c_interest;
      Scanner scanner = new Scanner (System. in);
      System.out.println("Enter the value of Principal = ");
      p = scanner.nextDouble();
      System. out. println("Enter the Annual Rate of Interest = ");
      r = scanner.nextDouble();
      System. out. println("Enter the Time (years) = ");
      t = scanner.nextDouble();
      s_interest = (p * r * t)/100;
      c_interest = p * Math.pow(1.0+r/100.0,t) - p;
      System.out.println("Simple Interest: "+s_interest);
      System.out. println("Compound Interest: "+c_interest);
   }
}

Output

Enter the value of Principal = 1500
Enter the Annual Rate of Interest = 8
Enter the Time (years) = 3
Simple Interest: 360.0
Compound Interest: 389.568000000000

Example 2

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

import java.util.*;
public class SimpleAndCompountInterest {
   public static void main(String []args){
      double p, r, t, s_interest, c_interest;
      p = 25000;
      r = 10;
      t = 4;
      System.out.println("Principal = "+p);
      System. out. println("Annual Rate of Interest = "+r);
      System. out. println("Time (years) = "+t);
      s_interest = (p * r * t)/100;
      c_interest = p * Math.pow(1.0+r/100.0,t) - p;
      System.out.println("Simple Interest: "+s_interest);
      System.out. println("Compound Interest: "+c_interest);
   }
}

Output

Principal = 25000.0
Annual Rate of Interest = 10.0
Time (years) = 4.0
Simple Interest: 10000.0
Compound Interest: 11602.500000000007
raja
Updated on 21-Feb-2022 11:47:39

Advertisements