Java Program to Calculate Simple Interest

The agenda of this article is to write a Java program to calculate simple interest. But, before doing it programmatically, let's understand how we calculate simple interest mathematically. The simple interest is a technique to determine the amount of interest gained on a principal amount at the specified interest rate for a given time period. Unlike compound interest, its principal amount does not change over time.

To calculate Simple Interest, we use the following formula −

Simple Interest (S.I) = Principal * Time * Rate / 100


Below is a demonstration of the same −

Input

Enter a Principle number: 100000
Enter an Interest rate: 5
Enter a Time period in years: 2


Output

Simple Interest : 1000


Calculate Simple Interest in Java

We are going to use the following ways to calculate Simple Interest in Java −

• By taking input of operands from user

• By initializing the values at the time of declaration

Let's discuss them one by one.

By taking input of operands from user

To take input from the keyboard, we need to create an instance of the Scanner class which provides various built-in methods for user input. For instance, we can use the 'nextDouble()' method if we need to enter a double value.

Syntax

Scanner nameOfinstance = new Scanner(System.in);


Example

In the following example, we will accept principal amount, rate of interest and time period from the keyboard using the Scanner class to find the simple interest.

import java.util.Scanner;
public class SimpleInterest {
public static void main (String args[]) {
// declaring principal, rate and time
double principal, rate, time, simple_interest;
// Scanner to take input from user
Scanner my_scanner = new Scanner(System.in);
System.out.println("Enter a Principal amount : ");
// to take input of principle
principal = my_scanner.nextDouble();
System.out.println("Enter an Interest rate : ");
// to take input of rate
rate = my_scanner.nextDouble();
System.out.println("Enter a Time period in years : ");
// to take input of time
time = my_scanner.nextDouble();
// calculating interest
simple_interest = (principal * rate * time) / 100;
// to print the result
System.out.println("The Simple Interest is : " + simple_interest);
double totalSum = simple_interest + principal;
System.out.println("Your total sum after gaining interest : " + totalSum);
}
}


Output

Enter a Principal amount :
50000
Enter an Interest rate :
5
Enter a Time period in years :
2
The Simple Interest is : 5000.0
Your total sum after gaining interest : 55000.0


By initializing the values at the time of declaration

It is the simplest way of calculating simple interest. We simply need to declare principal amount, rate of interest and time period as operands and initialize them with the value of our choice. Also, we require another variable to store the result of simple interest.

Example

The following example illustrates the practical implementation of what we have discussed above.

public class SimpleInterest {
public static void main (String args[]) {
// declaring and initializing principal, rate and time
double principal = 50000;
double rate = 5;
double time = 2;
// calculating interest
double simple_interest = (principal * rate * time) / 100;
// to print the result
System.out.println("The Simple Interest is : " + simple_interest);
double totalSum = simple_interest + principal;
System.out.println("Your total sum after gaining interest : " + totalSum);
}
}


Output

The Simple Interest is : 5000.0
Your total sum after gaining interest : 55000.0


Conclusion

In our daily lives, we can see many applications of simple interest such as loans, EMIs and FDs. Therefore, it is necessary to learn how to calculate it mathematically as well as programmatically. In this article, we have explained how to make a Java program to calculate the simple interest.

Updated on: 10-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Advertisements