Java Program to Calculate Simple Interest and Compound Interest

Java Program to Calculate simple interest and compound interest

Scanner Class and Operator in Java.

The java.util.Scanner class is used for reading input from various sources like the keyboard, file, or network socket. It provides methods for reading different data types, including strings, primitive types (int, double, etc.), and tokenized input.

On the other hand, Java doesn’t have a class called java.util.Operator. When performing mathematical operations on the input data, the java.util.Scanner class can be used in conjunction with a number of Java operators, such as +, -, *, /, etc.

To know more about Java program to calculate Simple Interest and Compound Interest  read the complete article.

Steps for writing a Java program to calculate Simple Interest and Compound Interest:

  • Define the variables for principal amount, rate of interest, and time period.
  • Calculate simple interest using the formula: (principal * rate * time) / 100.
  • Calculate compound interest using the formula: principal * (1 + rate / 100)^time.
  • Print the result of both simple and compound interest.

Let’s look at a Java program to Calculate simple Interest and Compound Interest

Example 1: Java Program to calculate Simple Interest

Run
public class Main {
  public static void main(String[] args) {

    System.out.print("Principal = 9000: ");
    double principal = 9000;

    System.out.print("rate of interest = 10 : ");
    double rate = 10;

    System.out.print("number of years = 5 : ");
    int years = 5;

    double simpleInterest = (principal * rate * years) / 100;
    System.out.println("Simple Interest: " + simpleInterest);
  }
}

Output

Principal = 9000: rate of interest = 10 : number of years = 5 : Simple Interest: 4500.0

Example 2 :Java Program to calculate Simple Interest and Compound Interest

Run
import java.util.*;
public class Main {
   public static void main(String []args){
      double p, r, t, s_interest, c_interest;
      p = 50000;
      r = 20;
      t = 5;
      System.out.println("Principal = "+p);
      System. out. println("Annual Rate of Interest = "+r);
      System. out. println("Time (years) = "+t);
      s_interest = (p * r * t)/100;
      c_interest = p * Math.pow(1.0+r/100.0,t) - p;
      System.out.println("Simple Interest: "+s_interest);
      System.out. println("Compound Interest: "+c_interest);
   }
}

Output

Principal = 50000.0
Annual Rate of Interest = 20.0
Time (years) = 5.0
Simple Interest: 50000.0
Compound Interest: 74415.99999999997

Example 3: Java Program to Calculate Simple Interest and Compound Interest

Run

import java.math.BigDecimal;
import java.math.MathContext;

public class Main {
   public static void main(String[] args) {
      double principal = 10000;
      double rate = 5;
      double time = 5;

      // Simple Interest
      double simpleInterest = (principal * rate * time) / 100;
      System.out.println("Simple Interest: " + simpleInterest);

      // Compound Interest
      BigDecimal compoundInterest = BigDecimal.valueOf(principal).multiply(BigDecimal.valueOf(1 + (rate / 100)).pow((int) time, MathContext.DECIMAL64));
      System.out.println("Compound Interest: " + compoundInterest);
   }
}

Output

Simple Interest: 2500.0
Compound Interest: 12762.81562500000

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription