Java program to calculate simple interest and compound interest

Here's an example Java program that calculates both simple interest and compound interest:

r‮fe‬er to:theitroad.com
import java.util.Scanner;

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

        // Get the principal amount, rate of interest, and time period from the user
        System.out.print("Enter the principal amount: ");
        double principal = input.nextDouble();

        System.out.print("Enter the rate of interest: ");
        double rate = input.nextDouble();

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

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

        // Calculate compound interest
        double compoundInterest = principal * Math.pow(1 + (rate / 100), time) - principal;

        // Print the results
        System.out.println("Principal amount: " + principal);
        System.out.println("Rate of interest: " + rate);
        System.out.println("Time period (in years): " + time);
        System.out.println("Simple interest: " + simpleInterest);
        System.out.println("Compound interest: " + compoundInterest);
    }
}

This program first prompts the user to enter the principal amount, rate of interest, and time period in years using the Scanner class. The program then calculates the simple interest and compound interest using the given formulae.

The simple interest formula is P * R * T / 100, where P is the principal amount, R is the rate of interest, and T is the time period in years.

The compound interest formula is P * (1 + R / 100) ^ T - P, where P, R, and T have the same meaning as in the simple interest formula.

Finally, the program prints the results to the console.

For example, if the user enters 1000 for the principal amount, 5 for the rate of interest, and 3 for the time period, the output of the program would be:

Principal amount: 1000.0
Rate of interest: 5.0
Time period (in years): 3.0
Simple interest: 150.0
Compound interest: 157.62877150000094