Simple interest program in Java
In this post, we will learn how to calculate Simple Interest(S.I.) using the Java programming language.
Simple Interest is a quick and easy method of calculating the interest amount for a particular principal amount of money at some rate of interest. So, simple interest is the simplest and easiest way to determine how much extra you'll have to pay for your loan. For example, when a person takes a loan of Rs. 6000, at a rate of 10 p.a. for three years, the person's interest for three years will be simple interest on the borrowed money.
Simple Interest Formula
The Simple Interest(S.I.) formula is given by-
SI = (P x T x R)/100
Where,
P is the principle amount
T is the time and
R is the rate
Algorithm of Simple Interest
- Declare Principal, Interest and Time of loans.
- Calculate the Simple Interest by applying the formula.
- Print the value of Simple Interest.
Java program to calculate Simple Interest
In the given Java program, we have defined the value of principal, rate, and time. We then use the formula for simple interest to compute the simple interest.
public class SimpleInterest
{
public static void main (String args[])
{
// define principal amount, rate, time and simple interest respectively
float principle, rate, time, si;
principle = 15000;
rate = 8;
time = 4;
si = (principle*rate*time)/100;
System.out.println("Simple Interest is: " +si);
}
}
Output of the above code:
Simple Interest is: 4800.0
Java program to calculate Simple Interest using function
Here is a simple Java program to calculate simple interest using a user-defined function. We define a function simpleInterest() to calculate the simple interest using the formula. Next, within the main function, we declare the variables for the principal amount, rate of interest, and time period, and, then call the user-defined function and print the result.
public class SimpleInterest
{
//User-defined program to find the simple interest
public static float simpleInterest(float principal, float rate, float time)
{
float interest = (principal*rate*time)/100;
return interest;
}
public static void main (String args[])
{
//Declare variables
float p, r, t;
p = 23000;
r = 5;
t = 6;
float si = simpleInterest(p,r,t);
System.out.println("Simple interest is : " + si);
//Declare variables
float p1, r1, t1;
p1 = 16000;
r1 = 6;
t1 = 8;
float si1 = simpleInterest(p1,r1,t1);
System.out.println("Simple interest is : " + si1);
}
}
Output of the above code:
Simple interest is : 6900.0
Simple interest is : 7680.0
Simple interest program in Java using Scanner
In the given Java program, we create an instance of the Scanner class to take the input from the user. Next, we declare variables for the principal amount, rate of interest, and time period and ask the user to provide inputs for these variables. Then, we call the user-defined method to calculate the simple interest and print the result.
import java.util.Scanner;
public class SIMain
{
//User-defined program to find the simple interest
public static float simpleInterest(float principal, float rate, float time)
{
float interest = (principal*rate*time)/100;
return interest;
}
public static void main(String args[])
{
//Taking input from the user
//Creating an instance of Scanner class
Scanner sc = new Scanner(System.in);
//Declaring variables
float p, r, t;
System.out.println("Enter the principal: ");
p = sc.nextFloat();
System.out.println("Enter the rate of interest: ");
r = sc.nextFloat();
System.out.println("Enter the time period: ");
t = sc.nextFloat();
sc.close();
//Calling method to calculate the simple interest
float si = simpleInterest(p,r,t);
System.out.println("Simple interest is : " + si);
}
}
Output of the above code:
Enter the principal: 25050
Enter the rate of interest: 7.3
Enter the time period: 10
Simple interest is : 18286.5
Related Articles
Java program to count the occurrences of each characterGCD of two numbers in Java
nth prime number in Java
Java random number between 1 and 10
Find the greatest of three numbers in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number