Simple Interest (S.I) is the method of calculating the interest amount for some principal amount of money.
To find Simple Interest:
Simple Interest = (P*R*T)/100
P = principal
R = interest rate (in percentage)
T = time duration (in years)
In this post we will learn to find Simple Interest using 4 different ways,
- Simple interest program in java using scanner
- Java program to calculate simple interest using interface
- Simple interest program in Java using Inheritance
- Java program to calculate simple interest using Constructor
Example 1: Simple Interest Program in Java Using Scanner
The Scanner class is used to get user input, and it is found in the java.util package. In this program we using Scanner class to accept Principal amount(p), Rate(r) and Time Period(t).
This is how we created Scanner class object Scanner sc = new Scanner(System.in); in our program.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
float p, r, t, SInterest;
Scanner sc = new Scanner(System.in);
//Accept user input for Principal
System.out.println("Enter the Value for Principal: ");
p = sc.nextFloat();
//Accept user input for rate of interest
System.out.println("Enter the Value for Rate of Interest: ");
r = sc.nextFloat();
//Accept user input for Time Period
System.out.println("Enter the Value for Time Period: ");
t = sc.nextFloat();
//close Scanner object after use
sc.close();
//To find Simple interest
SInterest = (p * r * t)/100;
System.out.println("Simple interest is: "+SInterest);
}
}
Output:
Enter the Value for Principal:
10000
Enter the Value for Rate of Interest:
6
Enter the Value for Time Period:
2
Simple interest is: 1200.0
Example 2: Java Program to Calculate Simple Interest using Interface
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Accept user input for Principal
System.out.println("Enter the Value for Principal: ");
float p = sc.nextFloat();
//Accept user input for rate
System.out.println("Enter the Value for Rate: ");
float r = sc.nextFloat();
//Accept user input for Year
System.out.println("Enter the Value for Year: ");
float y = sc.nextFloat();
Simple s = new Simple(p, r, y);
sInterestPrint(s.getInterest());
}
private static void sInterestPrint(float sIinterest) {
System.out.print(sIinterest);
}
}
interface IInterest {
float getInterest();
}
class Interest {
float principal, rate, years;
public Interest(float principal, float rate, float years) {
this.principal = principal;
this.rate = rate;
this.years = years;
}
float getInterest() {
return principal * rate * years;
}
}
class Simple extends Interest implements IInterest {
public Simple(float principal, float rate, float years) {
super(principal, years, rate);
}
public float getInterest() {
return super.getInterest() / 100;
}
}
Output:
Enter the Value for Rate:
5
Enter the Value for Year:
2
1000.0
Example 3: Simple interest program in Java using Inheritance
import java.util.Scanner;
// Class use to calculate the simple interest
class SimpleInterest {
public float CalculateSI(float x, float y, float z) {
return (x * y * z) / 100;
}
}
// This class will inherit `SimpleInterest` class
class PrintSimpleInterest extends SimpleInterest {
public void PrintInterest(float SInterest) {
System.out.println("\nSimple Interest = " + SInterest);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
System.out.println("Enter the principal: ");
float p = sc.nextFloat();
System.out.println("Enter the Time Period: ");
float y = sc.nextFloat();
System.out.println("Enter the Rate: ");
float r = sc.nextFloat();
PrintSimpleInterest SimpleInterest = new PrintSimpleInterest();
float SInterest = SimpleInterest.CalculateSI(p, y, r);
// To print Simple Interest
SimpleInterest.PrintInterest(SInterest);
}
}
Example 4: Java Program to Calculate Simple Interest using Constructor
In this program we use constructor to find the Simple Interest. Constructor is use to initialize the value for object attributes. constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory
import java.util.Scanner;
class SimpleInterest {
void CalculateSimpleInterest(float principal, float year, float rate) {
float SInterest;
// Calculate the simple interest
SInterest = (principal * year * rate) / 100;
System.out.println("\nSimple Interest = " + SInterest);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
SimpleInterest sIObject = new SimpleInterest();
System.out.println("Enter the principal: ");
float p = sc.nextFloat();
System.out.println("Enter the Time Period: ");
float y = sc.nextFloat();
System.out.println("Enter the Rate: ");
float r = sc.nextFloat();
//Closes scanner object after use
sc.close();
// It will produce the final output
sIObject.CalculateSimpleInterest(p, y, r);
}
}
output:
Enter the principal:
10000
Enter the Time Period:
5
Enter the Rate:
2
Simple Interest = 1000.0