# How to calculate Compound Interest in Java ?.

Updated on September 17, 2014

In this Java tutorial, you will learn how to calculate Compound Interest in Java. In this Java program, there is a Java class named as, CompoundInterestDemo. This Java class has main method with signature - public static void main( String [ ] args ) { }. This main method is called by Java Virtual Machine ( JVM ) when you run program. Below is the Java source code and video tutorial, which demonstrates how to calculate Compound Interest in Java.

## Video tutorial - How to calculate Compound Interest in Java ?

A simple video tutorial, that demonstrates how to calculate Compound Interest in Java.

## How it works -

1. Import java.util.Scanner to use Scanner class methods such as, nextInt and nextDouble.
2. Create a Scanner instance, by using System.in object passing into constructor of Scanner class. Here, System.in helps scanner class to read user inputs from the console.
3. Create 4 double variables such as, principal, rate, time and compoundInterest. Here, principal holds the principal amount, rate holds rate of interest, time holds time period for compoundInterest and compoundInterest hold the calculates compound interest.
4. Prompt the user to enter three values for, principal , rate and time. Use Scanner class method named as, nextDouble. This method reads a double value entered by user on the Java console.
5. Formula to calculate compound interest - principal * Math.pow((1 + rate/100),time). Math class has a static method called as, pow. This method takes two arguments which, returns the value of the first argument raised to the power of the second argument.

6. Print compound interest on the Java console.

## Output of the Java program -

Upon running below Java program, it prompts user to enter value of principal, rate and time. As soon as, user enter these values and hit enter key, the compound interest is calculated and printed on the Java console. As shown in the image below -

## Java source code - How to calculate Compound Interest in Java ?.

```// Step 1 - Import java.util.Scanner to use Scanner class
// methods such as, nextInt and nextDouble.
import java.util.Scanner;

public class CompoundInterestDemo {

public static void main(String[] args) {

// Step 2 - Create a Scanner instance, by using
// System.in object passing into constructor of
// Scanner class. Here, System.in helps scanner
// class to read user inputs from the console.
Scanner scanner = new Scanner(System.in);

// Step 3 - Create 4 double variables such as,
// principal, rate, time and compoundInterest.
// Here, principal holds the principal amount,
// rate holds rate of interest, time holds time
// period for compoundInterest and compoundInterest
// hold the calculates compound interest.
double principal = 0;
double rate = 0;
double time = 0;
double compoundInterest = 0;

// Step 4 - Prompt the user to enter three values for,
// principal , rate and time. Use Scanner class method
// named as, nextDouble. This method reads a double value
// entered by user on the Java console.
// Enter principal amount.
System.out.print("Enter the Principal amount : ");
principal = scanner.nextDouble();

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

// Enter time of interest.
System.out.print("Enter the Time : ");
time = scanner.nextDouble();

// Step 5 - Formula to calculate compound interest -
// principal * Math.pow((1 + rate/100),time).
// Math class has a static method called as, pow.
// This method takes two arguments which, Returns the
// value of the first argument raised to the power of the second argument.
compoundInterest = principal * Math.pow((1 + rate/100),time);

// Step 6 - Print compound interest on the Java console.
System.out.println("");
System.out.println("The Compound Interest is : " + compoundInterest);

}

}
```

5

110

0

9