Write a Simple Interest Program in Java using the SI formula.
Simple Interest is the sum of money that must be paid as an additional percentage of the principal money to the lender by the person who borrowed it.
Formula to Calculate Simple Interest:
Simple Interest = (P × R × T)/100
Here, P = Principal Amount, R = Rate of Interest and T = Time.
Example 1:
Given: P = 20202, R = 2.5 and T = 3
Simple Interest (SI) = (P * R * T)/100
SI = (20202 * 2.5 * 3)/100 = 151515/100
SI = 1515.15
Example 2:
Given: P = 7000, R = 50 and T = 2
Simple Interest (SI) = (P * R * T)/100
SI = (7000 * 50 * 2)/100 = 700000/100
SI = 7000
Algorithm
1. Enter Principal, Rate of Interest and Time.
2. Calculate simple interest using the formula SI = (P * R * T)/100.
3. Print the Simple Interest.
Here is source code of the Java program to calculate the simple interest. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
import java.util.Scanner;
public class Simple_Interest
{
public static void main(String args[])
{
float p, r, t;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
float si;
si = (r * t * p) / 100;
System.out.print("The Simple Interest is : " + si);
}
}
1. Take the user input for Principal, Rate, and Time and store it in variables p, r and t.
2. Find the simple interest using the formula SI = (P * R * T)/100
3. Print the Simple Interest.
Time complexity: O(1)
Since the program runs in constant time, the time complexity for calculating simple interest is O(1).
Space complexity: O(1)
According to this program, the space complexity is O(1) and no array or large chunk of memory is required for the calculation of simple interest and amount.
Testcase 1: In this case, enter P=20202, R=2.5, and T=3 as the parameters to calculate simple interest.
$ javac Simple_Interest.java $ java Simple_Interest Enter the Principal : 20202 Enter the Rate of interest : 2.5 Enter the Time period : 3 The Simple Interest is : 1515.15
Testcase 2: In this case, enter P=7000, R=50, and T=2 as the parameters to calculate simple interest.
$ javac Simple_Interest.java $ java Simple_Interest Enter the Principal : 7000 Enter the Rate of interest : 50 Enter the Time period : 2 The Simple Interest is : 7000
To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.
- Check Programming Books
- Practice Information Technology MCQs
- Practice BCA MCQs
- Apply for Java Internship
- Check Java Books