Calculate Simple Interest in Java

Exercise:

Write a Java Program to calculate simple interest .

Click Here to View the Solution!
import java.util.Scanner;

public class SimpleInterest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        // take input from users
        System.out.print("Principal: ");
        double principal = input.nextDouble();
        System.out.print("Time: ");
        double time = input.nextDouble();
        System.out.print("Rate: ");
        double rate = input.nextDouble();
        rate = rate/100;

        //Formula to calculate Interest
        double interest = (principal * time * rate);
        System.out.println("Simple Interest: " + interest);

        input.close();
    }
}
Click Here to View the Output!
Principal: 5000                                                
Time: 3                                                        
Rate: 12                                                       
Simple Interest: 1800.0    
Click Here to View the Explanation!
  • This program is used to perform the calculation of simple interest using a formula and Java methods.
  • Initially, a Scanner object of the Scanner class is created that is used to take input from the user.
  • The program will request the user to enter the values of principal, rate and time. Each of which are stored in double type variables.
  • Since, the interest rate is a percentage, the value is divided by 100 to provide a decimal value.
  • Finally, a formula to calculate the simple interest is stored in the variable interest as follows: Interest = (principal * time * rate)
  • Finally, the output is printed on the screen.
%d bloggers like this: