Write a Java Program to Calculate simple interest and compound interest

As a Java programmer, it is essential to understand how to calculate simple and compound interest.

In this tutorial, we will discuss the concepts of simple and compound interest and provide you with a Java program to calculate both.


Simple Interest

Simple interest is the interest earned on a principal amount for a specified period of time.

The formula for calculating simple interest is:

Simple Interest = (Principal * Rate * Time) / 100

Where, Principal: The amount of money borrowed or invested Rate: The interest rate (in percentage) Time: The duration for which the principal amount is borrowed or invested

Java Program to calculate Simple Interest:

import java.util.Scanner;

public class SimpleInterest {
   public static void main(String args[]){
      float p, r, t, si;

      Scanner input = new Scanner(System.in);

      System.out.println("Enter the principal amount:");
      p = input.nextFloat();

      System.out.println("Enter the interest rate:");
      r = input.nextFloat();

      System.out.println("Enter the time period in years:");
      t = input.nextFloat();

      si = (p * r * t) / 100;

      System.out.println("The simple interest is: " + si);
   }
}

Compound Interest

Compound interest is the interest earned on a principal amount, including the previously earned interest.

The formula for calculating compound interest is:

Compound Interest = Principal * (1 + Rate/100) ^ Time - Principal

Where, Principal: The amount of money borrowed or invested Rate: The interest rate (in percentage) Time: The duration for which the principal amount is borrowed or invested

Java Program to calculate Compound Interest:

import java.util.Scanner;

public class CompoundInterest {
   public static void main(String args[]){
      float p, r, t, ci;

      Scanner input = new Scanner(System.in);

      System.out.println("Enter the principal amount:");
      p = input.nextFloat();

      System.out.println("Enter the interest rate:");
      r = input.nextFloat();

      System.out.println("Enter the time period in years:");
      t = input.nextFloat();

      ci = (float) (p * Math.pow(1 + r / 100, t) - p);

      System.out.println("The compound interest is: " + ci);
   }
}

Conclusion

Calculating simple and compound interest is a fundamental concept in finance.

As a Java programmer, you can use the above Java programs to calculate simple and compound interest for different inputs.

With these programs, you can easily calculate the interest amount for different financial scenarios.