In this tutorial, you will learn how to write a Java program to calculate simple interest. First, we will understand the formula for simple interest.
Next, we will write a basic program by assigning values directly to variables. Finally, we will build an interactive program using the Scanner class to accept the principal amount, rate of interest, and time from the user.
Understanding the Simple Interest Formula
Before jumping into the code, let’s look at the mathematical formula for calculating simple interest:
Simple Interest = (P × R × T) / 100
Where:
- P (Principal): The initial amount of money borrowed or invested.
- R (Rate): The annual rate of interest (in percentage).
- T (Time): The time period for which the money is borrowed or invested (in years).
For example, suppose the principal amount is 10,000, the annual interest rate is 5%, and the time period is 2 years.
To find the simple interest, we multiply the principal by the rate and the time, which gives 100,000. When we divide that product by 100, we get a simple interest of 1,000.
SI = (10,000 × 5 × 2) / 100 = 1,000
Java Program to Calculate Simple Interest with Pre-assigned Values
Problem Statement: Write a Java program to calculate simple interest when the principal amount, rate of interest, and time are already given.
In this first approach, we assign values directly to variables inside the code. This is great for understanding the basic syntax of Java calculations.
Program Code:
// Java program to calculate simple interest.
public class SimpleInterest {
public static void main(String[] args) {
// Declare and initialize variables
double principal = 10000.0; // Principal amount
double rate = 5.0; // Annual interest rate in %
double time = 2.0; // Time in years
// Calculate simple interest
double simpleInterest = (principal * rate * time) / 100;
// Display the results
System.out.println("--- Simple Interest Details ---");
System.out.println("Principal Amount: " + principal);
System.out.println("Rate of Interest: " + rate + "%");
System.out.println("Time: " + time + " years");
System.out.println("Simple Interest: " + simpleInterest);
}
}Expected Output:
--- Simple Interest Details --- Principal Amount: 10000.0 Rate of Interest: 5.0% Time: 2.0 years Simple Interest: 1000.0
In this example program:
- The principal variable stores the initial amount of money called the principal amount.
- The rate variable stores the annual interest rate as a percentage.
- The time variable stores the duration of the loan in years.
- Then, we have applied the simple interest formula using the variables above.
- The statement
System.out.println;prints the final calculated simple interest to the console.
Java Program to Calculate Simple Interest Using User Input (Scanner)
Problem Statement: Write an interactive Java program to calculate simple interest by allowing the user to input their own principal, rate, and time values dynamically.
Program Code:
// Java program to calculate simple interest using Scanner.
import java.util.Scanner;
public class SimpleInterestUsingScanner {
public static void main(String[] args) {
// Create a Scanner object to read input from the console.
Scanner sc = new Scanner(System.in);
// Prompt user for Principal amount.
System.out.print("Enter principal amount: ");
double principal = sc.nextDouble();
// Prompt user for Rate of interest
System.out.print("Enter rate of interest: ");
double rate = sc.nextDouble();
// Prompt user for Time period.
System.out.print("Enter time in years: ");
double time = sc.nextDouble();
// Calculate simple interest.
double simpleInterest = (principal * rate * time) / 100;
// Display the calculated result.
System.out.println("Simple Interest: " + simpleInterest);
// Close the scanner to prevent memory leaks.
sc.close();
}
}Expected Output:
Enter principal amount: 10000 Enter rate of interest: 5 Enter time in years: 2 Simple Interest: 1000.0
In this example program:
- The Scanner class is imported from the java.util package so that the program can accept input from the user.
- A Scanner object is created to read data entered through the keyboard.
- The nextDouble() method reads the principal amount, rate of interest, and time entered by the user, storing them in the principal, rate, and time variables, respectively.
- The simple interest formula is applied to the variables, and the final value is stored in a variable named simpleInterest.
- The calculated result is printed to the console using System.out.println().
- Finally, the sc.close() statement closes the Scanner object to release system resources once the input operation is complete.




