Table of contents
1.
Introduction
2.
Formula
3.
Example
4.
Java Program for Simple Interest
5.
Time and Space Complexity  
5.1.
Time Complexity
5.2.
Space Complexity
6.
Runtime Test Cases  
7.
Frequently Asked Questions
7.1.
Can we calculate simple interest for months instead of years?
7.2.
What happens if the interest rate is given as a decimal?
7.3.
Can I use float instead of double for calculation?
8.
Conclusion
Last Updated: Mar 8, 2025
Easy

Java Program to Calculate Simple Interest

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A  Java program to calculate simple interest helps in determining the interest earned on a principal amount over a specific period. This program takes user input for the principal amount, interest rate, and time duration to compute the interest. Simple interest calculations are widely used in banking and financial applications. 

In this article, we will discuss the implementation, logic, and example code to perform this calculation in  Java efficiently.

Formula

Simple interest is calculated using the following formula:

Simple Interest=(Principal×Rate×Time)/100

Where:

  • SI = Simple Interest
     
  • P = Principal Amount (Initial sum of money)
     
  • R = Rate of Interest per annum (in percentage)
     
  • T = Time period (in years)
     

This formula helps in determining the extra amount to be paid along with the principal amount after a specified time.

Example

Let's consider an example:

  • Principal Amount (P) = 5000
     
  • Rate of Interest (R) = 5%
     
  • Time Period (T) = 2 years
     

Using the formula:

SI=5000×5×2​/ 100
SI=50000​/ 100


So, the simple interest is 500. Now, let’s implement this in  Java.

Java Program for Simple Interest

Below is the complete  Java program to calculate simple interest:

import java.util.Scanner;
public class SimpleInterest {
    public static void main(String[] args) {
        // Creating Scanner object for user input
        Scanner sc = new Scanner(System.in);
        // Taking input from the user
        System.out.print("Enter Principal amount: ");
        double principal = sc.nextDouble();
        System.out.print("Enter Rate of Interest: ");
        double rate = sc.nextDouble();
        System.out.print("Enter Time period (in years): ");
        double time = sc.nextDouble();
        
        // Calculating simple interest
        double simpleInterest = (principal * rate * time) / 100;
        // Displaying the result
        System.out.println("Simple Interest: " + simpleInterest);
        // Closing the scanner object
        sc.close();
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Explanation of the Code

  1. The program uses the Scanner class to take input from the user.
     
  2. It asks the user for the Principal amount, Rate of Interest, and Time period.
     
  3. It then calculates the simple interest using the formula.
     
  4. Finally, it displays the computed Simple Interest.
     

Sample Output

Enter Principal amount: 5000
Enter Rate of Interest: 5
Enter Time period (in years): 2
Simple Interest: 500.0


This program allows users to input their values and get the simple interest calculated instantly.

Time and Space Complexity  

Time Complexity

The time complexity of the program is O(1), which means it takes constant time to execute. This is because the program performs a fixed number of operations regardless of the input size. Let’s see why it is like this:  

  • Reading inputs using `Scanner` takes constant time.  
     
  • The calculation `(principal  rate  time) / 100` is a single arithmetic operation.  
     
  • Printing the result is also a constant-time operation.  


Since there are no loops or recursive calls, the program runs in constant time.  

Space Complexity

The space complexity of the program is also O(1), meaning it uses a constant amount of memory. Let’s discuss why:

  • The program uses a few variables (`principal`, `rate`, `time`, `simpleInterest`) to store inputs & results.  
     
  • The `Scanner` object is the only additional memory used, but it doesn’t grow with input size.  


In summary, the program is highly efficient in terms of both time & space.  

Runtime Test Cases  

To ensure the program works correctly, let’s test it with different inputs. Below are some test cases & their expected outputs:  
 

Test Case 1:  

Input:  

  • Principal (P) = 10,000  
  • Rate (R) = 5  
  • Time (T) = 2  

Output: Simple Interest (SI) = (10,000 × 5 × 2) / 100 = 1,000  
 

Test Case 2:  

Input:  

  • Principal (P) = 20,000  
  • Rate (R) = 7.5  
  • Time (T) = 3  

Output: Simple Interest (SI) = (20,000 × 7.5 × 3) / 100 = 4,500  
 

Test Case 3:  

Input:  

  • Principal (P) = 50,000  
  • Rate (R) = 10  
  • Time (T) = 0.5  

Output: Simple Interest (SI) = (50,000 × 10 × 0.5) / 100 = 2,500  


Test Case 4:  

Input:  

  • Principal (P) = 0  
  • Rate (R) = 5  
  • Time (T) = 2  

Output: Simple Interest (SI) = (0 × 5 × 2) / 100 = 0  
 

Test Case 5:  

Input:  

  • Principal (P) = 15,000  
  • Rate (R) = 0  
  • Time (T) = 5  

Output: Simple Interest (SI) = (15,000 × 0 × 5) / 100 = 0  


These test cases cover various scenarios, including zero values for principal, rate, & time. They help verify the correctness & robustness of the program.  

Frequently Asked Questions

Can we calculate simple interest for months instead of years?

Yes, you can calculate interest for months by converting the time period into years. For example, 6 months would be 0.5 years.

What happens if the interest rate is given as a decimal?

If the interest rate is given as a decimal (e.g., 0.05 for 5%), you do not need to divide by 100 in the formula.

Can I use float instead of double for calculation?

Yes, you can use float, but double provides better precision for financial calculations.

Conclusion

In this article, we discussed how to write a  Java program to calculate simple interest using the formula SI = (P × R × T) / 100, where P is the principal amount, R is the rate of interest, and T is the time period. This program helps in financial calculations and strengthens basic Java programming concepts.

Live masterclass