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
- The program uses the Scanner class to take input from the user.
- It asks the user for the Principal amount, Rate of Interest, and Time period.
- It then calculates the simple interest using the formula.
- 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.