Simple Interest Program in Java

Simple interest program in Java | Simple interest is a quick and easy method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments.

The formula for simple interest is given as,

Simple Interest = (principal amount × interest rate × time) / 100

Procedures to write Simple interest program in Java

1) Define class and the main method
2) Declare variables for taking inputs:- principal amount, time, and interest rate
3) import Scanner class of util package to read inputs
4) Read inputs from end-user and store them in the declared variables
5) Calculate simple interest using the formula, and store it in a variable
6) Display the simple interest
7) Close the Scanner class object

Java program to calculate Simple interest

Program description:- Write a Java program to calculate the simple interest.

import java.util.Scanner;
 
public class SimpleInterest {
   public static void main(String[] args) {

       // declare variables
       double principalAmount = 0.0; 
       double rate = 0.0;
       double time = 0.0;
       double interest = 0.0;

       // create object of Scanner class
       Scanner scan = new Scanner(System.in);

       // read inputs
       System.out.print("Enter principal amount:: ");
       principalAmount = scan.nextDouble();
       System.out.print("Enter time (in months):: ");
       time = scan.nextDouble();
       System.out.print("Enter  the  interest rate (per year):: ");
       rate = scan.nextDouble();

       // calculate simpleInterest
       interest = (principalAmount * rate * time) / 100;

       // display result
       System.out.println("Simple interest = "+interest);
       System.out.println("Total amount to pay = "+ 
                          (principalAmount+interest));

       // close scan 
       scan.close();
   }
}

Output:-

Enter principal amount:: 10000
Enter time (in months):: 60
Enter the interest rate (per year):: 3.9
Simple interest = 23400.0
Total amount to pay = 33400.0

Also see:- Compound interest program in Java

It is good practice to initialize the local variables with their default values at the time of variable declaration. So at the variable declaration time, we initialize those variables with the default value. The variables principalAmount, time, rate, and interest are of double data type and 0.0 is the default value of the double data type.

The nextDouble() method of Scanner class is used to read the double data type as input values from the end-user. The nextDouble() method read the string value at the runtime as a floating-point number.

The * and / operators are used for multiplication and division. Later, the result is stored in the interest variable, and it is displayed to the console.

At last, we closed the Scanner class object using close() method. It closes the connection between the Java program and the keyboard.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply