In the previous article, we have seen Java Program to Calculate Total Distance Covered If Speed Given

In this article we are going to see how calculate Compound Interest using Java programming language.

## Java Program to Calculate Compound Interest

Before jumping into the program let’s know how we find the compound interest.

Compound Interest= p * Math.pow(((1 + (r / 100)), t)

where,

- p = principal
- r = annual rate of interest
- t = Number of years

Let’s see different ways to find compound interest.

### Method-1: Java Program to Calculate Compound Interest By Using Static Input Value

**Approach:**

- Initialize variables for principal amount, rate of interest and number of years.
- Calculate the compound interest according to the formula.
- Print the result.

**Program**:

public class Main { public static void main(String[] args) { //declaring value of p, t, r double p = 10000; double r = 6.5; double t = 10; //finding compound interest using formula double ci = p * Math.pow((1 + (r / 100)), t); System.out.println("The compound interest is: " + ci); } }

Output: The compound interest is: 18771.37465269359

### Method-2: Java Program to Calculate Compound Interest By Using User Input Value

**Approach:**

- Create Scanner class object.
- Take user input for principal amount, rate of interest and number of years.
- Calculate the compound interest according to the formula.
- Print the result.

**Program**:

import java.util.Scanner; public class Main { public static void main(String[] args) { //Scanner class object created Scanner sc = new Scanner(System.in); //taking user input of p, t, r System.out.print("Enter the principal amount: "); double p = sc.nextDouble(); System.out.print("Enter the rate of interest: "); double r = sc.nextDouble(); System.out.print("Enter the time period: "); double t = sc.nextDouble(); // calculating compound interest double ci = p * Math.pow((1 + (r / 100)), t); System.out.println("The compound interest is: " + ci); } }

Output: Enter the principal amount: 9800 Enter the rate of interest: 2 Enter the time period: 8 The compound interest is: 11482.261933822205

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

**Related Java Programs:**