English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java Input Output (I/O)

Java Reader/Writer

Java other topics

Java program calculates simple and compound interest

    Comprehensive Java Examples

In this example, we will learn how to calculate simple and compound interest in Java.

To understand this example, you should understand the followingJava programmingTopic:

Example1:Java program calculates simple interest

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    //Create an object of the Scanner class
    Scanner input = new Scanner(System.in);
    //Accept user input
    System.out.print("Enter the principal: ");
    double principal = input.nextDouble();
    System.out.print("Enter the interest rate: ");
    double rate = input.nextDouble();
    rate = rate/100;
    System.out.print("Enter the time: ");
    double time = input.nextDouble();
    double interest = (principal * time * rate) / 100;
    System.out.println("Principal: ", + principal);
    System.out.println("Interest Rate: ", + rate);
    System.out.println("Time: ", + time);
    System.out.println("Simple interest: ", + interest);
    input.close();
  }
}

Output Result

Enter the principal: 1000
Enter the interest rate: 8
Enter the time: 2
Principal: 1000.0
Interest Rate: 8.0
Time: 2.0
Simple interest: 160.0

In the above example, we used the Scanner class to receive input from the user principal,rate and time. Then, we use the simple interest formula to calculate the simple interest.

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

Example2:Java calculates compound interest

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    //Create an object of the Scanner class
    Scanner input = new Scanner(System.in);
    //Accept user input
    System.out.print("Enter the principal: ");
    double principal = input.nextDouble();
    System.out.print("Enter the interest rate: ");
    double rate = input.nextDouble();
    System.out.print("Enter the time: ");
    double time = input.nextDouble();
    System.out.print("Enter the number of compounding periods: ");
    int number = input.nextInt();
    double interest = principal * (Math.pow((1 + rate/100), 	(time * number))) - principal;
    System.out.println("Principal: ", + principal);
    System.out.println("Interest Rate: ", + rate);
    System.out.println("Time: ", + time);
    System.out.println("Number of Compounding Periods: ", + number);
    System.out.println("Compound Interest: ", + interest);
    input.close();
  }
}

Output Result

Enter the principal: 1000
Enter the interest rate: 10
Enter the time: 3
Enter the number of compounding periods: 1
Principal: 1000.0
Interest Rate: 10.0
Time: 3.0
Number of Compounding Periods: 1
Compound Interest: 331.00000000000045

In the above example, we use the compound interest formula to calculate compound interest.

Here, we useMath.pow()Method to calculate the power of a number.

Comprehensive Java Examples