Introduction

Java Program to Calculate Simple interest

In this program, you'll learn to calculate the simple interest and display final value on screen/terminal

To understand this example, you should have the knowledge of the following Java programming topics:

Calculate Simple Interest

A java program that calculate the simple interest by getting user input for priniple amount, Rate of interest, Time Period.

import java.util.Scanner;
public class simpleInterest {
    public static void main(String[] args){
        int principleAmount,time;
        float interestRatePerAnnum;
        // instance of Scanner class
        Scanner input = new Scanner(System.in);
        // taking user input
        System.out.print("Enter the principle amount : ");
        principleAmount = input.nextInt();
        System.out.print("Enter the rate of interest(per annum): ");
        interestRatePerAnnum = input.nextFloat();
        System.out.print("Enter the time period(y): ");
        time = input.nextInt();

        float simpleInterest = (principleAmount * interestRatePerAnnum * time)/100;

        System.out.println("Simple Interest: " + simpleInterest);

        float totalAmountToReceive = principleAmount + simpleInterest;
        System.out.println("Total amount person receive after " + time + " year" + ": " + totalAmountToReceive);
    }
}

Output

Enter the principle amount : 1500
Enter the rate of interest(per annum): 8
Enter the time period(y): 5
Simple Interest: 600.0
Total amount person receive after 5 year: 2100.

To Calculate Simple interest , SimpleInterest = (P*R*T)/100

here, P = principleAmount R = Rate of interest (%) T = Time Period (per annum),

We need to take user input for following (P,R,T) and then apply the formulae using *,/ operator. And then, print the output to user's terminal using println() funciton.

Don't know how to take input from the user ? Look at this examples

Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, learn more here.

Try Yourself

  1. Change Inputs: Calculate the interest for Principal = 5000, Rate = 4.5%, Time = 10 years.
  2. Total Amount: Modify the code to print only the total amount (Principal + Interest).
  3. Zero Interest: What happens if the rate is 0?
  4. Double Precision: Use double instead of float for more precision. Does the output change?
  5. Brain Twister: If the time is given in months (e.g., 18 months), how would you modify the formula? Hint: Divide months by 12 to get years.