Simple Interest Calculator in Java

 

The purpose of this program is to calculate the simple interest earned on an investment over a specific period of time. Simple interest is calculated using the formula:

Simple Interest (SI) = (Principal × Rate × Time) / 100

In this formula:

  • Principal: The initial amount of money invested or loaned.
  • Rate: The annual interest rate (in percentage).
  • Time: The time period for which the money is invested or borrowed (in years).

Java Code


public class SimpleInterestCalculator {
    public static void main(String[] args) {
        // Declare variables
        double principal;
        double rate;
        double time;
        double simpleInterest;

        // Create a Scanner object for user input
        java.util.Scanner scanner = new java.util.Scanner(System.in);

        // Get user input
        System.out.print("Enter the principal amount: ");
        principal = scanner.nextDouble();

        System.out.print("Enter the rate of interest (in %): ");
        rate = scanner.nextDouble();

        System.out.print("Enter the time (in years): ");
        time = scanner.nextDouble();

        // Calculate simple interest
        simpleInterest = (principal * rate * time) / 100;

        // Display the result
        System.out.println("The simple interest is: " + simpleInterest);

        // Close the scanner
        scanner.close();
    }
}
        

Program Structure Explanation

The program is structured as follows:

  • Class Declaration: The program is encapsulated in a public class named SimpleInterestCalculator.
  • Main Method: The main method is the entry point of the program, where the execution begins.
  • Variable Declarations: Variables for principal, rate, time, and simple interest are declared.
  • Input Handling: A Scanner object is created to take user input for the principal amount, rate of interest, and time period.
  • Calculation: Simple interest is calculated using the provided formula.
  • Output: The calculated simple interest is displayed to the user.
  • Resource Management: The scanner is closed to prevent resource leaks.

How to Run the Program

To run the program, follow these steps:

  1. Ensure you have Java Development Kit (JDK) installed on your machine.
  2. Open a text editor or an Integrated Development Environment (IDE) like Eclipse or IntelliJ IDEA.
  3. Create a new file and name it SimpleInterestCalculator.java.
  4. Copy and paste the provided Java code into the file.
  5. Save the file and open a command prompt or terminal.
  6. Navigate to the directory where the file is saved.
  7. Compile the program using the command: javac SimpleInterestCalculator.java.
  8. Run the program using the command: java SimpleInterestCalculator.
  9. Follow the prompts to enter the principal, rate, and time, and view the calculated simple interest.

 

One Reply to “Simple Interest Calculator in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *