Java program to find simple interest

Let’s learn java program to find simple interest.

Java program to find simple interest

Simple interest program in java is the one of the frequently asked question in interview.

Simple interest is used to enumerate interest on loan in banks. It is calculated by using formula,

Also read – java reverse string array

Simple Interest = (P x R x T) / 100

P – is principal amount.

R – is rate per annum.

T – is time in years.

In the below simple interest program in java first user enters principal amount, rate and time in years using nextFloat() method of Scanner class.

These three values are stored in principal, rate and time variables of data type float.

Then find interest value in java using above formula.

Let’s see simple interest program in java netbeans.

import java.util.Scanner;
public class SimpleInterestJavaProgram
{
   public static void main(String[] args)
   {
      float principal, rate, time;
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter principal amount : ");
      principal = sc.nextFloat();
      System.out.print("Please enter rate annually : ");
      rate = sc.nextFloat();
      System.out.print("Please enter time(in years) : ");
      time = sc.nextFloat();
      float simpleInterest;
      simpleInterest = (principal * time * rate) / 100;
      System.out.println("The Simple Interest is : " + simpleInterest);
      sc.close();
   }
}


Output:

java program to find simple interest

program to calculate compound interest

Now let’s learn java program to calculate compound interest.

To calculate compound interest here’s the formula.

P (1 + R/n) (nt) – P

In compound interest formula,

P – is principal amount.

R – is annual interest rate.

t – is time the money is invested or borrowed.

n – number of times interest is compounded. For example if ‘t’ is in years and compounded monthly then ‘n’ value is 12 and if compounded quarterly then ‘n’ value is 4.

For example sachin has ₹ 200000 rupees deposited in ABC bank as fixed deposit.

The fixed deposit has an annual interest of 6 percent compounded monthly.

Then compound interest after 6 years is,

P = ₹ 200000

R = 6/100 = 0.06

n = 12

t = 6

Compound interest = 200000 (1 + 0.06 / 12) (12 * 6) – 200000 = ₹ 286408.85

Here’s the java program to find compound interest or java program to calculate compound interest.

public class CompoundInterestDemo 
{
   public void calculateCompound(int p, int t, double r, int n) 
   {
      double number = p * Math.pow(1 + (r / n), n * t);
      double interest = number - p;
      System.out.println("Compound interest after " + t + " years: " + interest);
      System.out.println("Money after " + t + " years: " + number);
   }
   public static void main(String[] args) 
   {
      CompoundInterestDemo obj = new CompoundInterestDemo();
      obj.calculateCompound(200000, 6, .06, 12);
   }
}


Output:

Compound interest after 6 years: 86408.8556983287
Money after 6 years: 286408.8556983287


So this is all about java program to find simple interest and compound interest.

I hope you have understood above java programs.