Java Program to Calculate Simple Interest

Problem Description

Write a Simple Interest Program in Java using the SI formula.

What is Simple Interest in Java?

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.

Problem Solution

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

advertisement
advertisement

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.

Program/Source Code

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.

Note: Join free Sanfoundry classes at Telegram or Youtube
  1. import java.util.Scanner;
  2. public class Simple_Interest
  3. {
  4.     public static void main(String args[]) 
  5.     {
  6.         float p, r, t;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter the Principal : ");
  9.         p = s.nextFloat();
  10.         System.out.print("Enter the Rate of interest : ");
  11.         r = s.nextFloat();
  12.         System.out.print("Enter the Time period : ");
  13.         t = s.nextFloat();
  14.         float si;
  15.         si = (r * t * p) / 100;
  16.         System.out.print("The Simple Interest is : " + si);
  17.     }
  18. }
Program Explanation

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.

advertisement
Runtime Test Cases

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”.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.