Calculate Simple Interest in Java
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// create an object of Scanner class
Scanner input = new Scanner(System.in);
// take input from users
System.out.print("Enter the principal: ");
double principal = input.nextDouble();
System.out.print("Enter the rate: ");
double rate = input.nextDouble();
System.out.print("Enter the time: ");
double time = input.nextDouble();
double interest = (principal * time * rate) / 100;
System.out.println("Principal: " + principal);
System.out.println("Interest Rate: " + rate);
System.out.println("Time Duration: " + time);
System.out.println("Simple Interest: " + interest);
input.close();
}
}
Output
Enter the principal: 1000
Enter the rate: 8
Enter the time: 2
Principal: 1000.0
Interest Rate: 8.0
Time Duration: 2.0
Simple Interest: 160.0
In the above example, we have used the Scanner class to take principal, rate, and time as input from the user. We then use the formula of simple interest to compute the simple interest.
Simple Interest = (Principal * Rate * Time) / 100