# Java Program to calculate compound interest

December 28, 2022, Learn eTutorial
1291

To write a java program to find the compound interest, We have to read the principal amount, interest per year, and time period, number of times the interest is compounded from the user.

## How we calculate compound interest?

We can calculate the compound interest by using the formula,

CI = P (1 + R/n) (nt) - P

where,

• P is the Principal amount.
• R is the Interest rate per Annum.
• t is the time period
• n is the number of times the interest is compounded per t.

## How to read a double value in java?

For reading a double value, we can use nextDouble() function in the java Scanner class. In this program, we have to read the principal amount, interest rate, time as double. So we have to write the code as follows:

`System.out.print("Enter the Principal amount : ");`
`p = sc.nextDouble();`

## How to find compound interest using Java program?

First, we have to declare the class CInterest.Then open the main function. Then declare the variables 'p, r, t, ci' as double and 'n' as an integer. Read the principal amount from the user into the variable 'p'. Read the rate of interest per annum in the variable 'r'. Read the time period from the user into the variable 't'. Read the number of times the interest is compounded into the variable 'n'. Then calculate compound interest as  ci = p * Math.pow(1 + (r / n), n * t). Then display compound interest as 'ci' using `system.out.println()` function.

## ALGORITHM

STEP 1: Declare the class CInterest with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the variables p,r,t, ci as double and n as an integer.

STEP 4: Read the principal amount from the user into the variable p.

STEP 5: Read the rate of interest into the variable r.

STEP 6: Read the time period in the variable t.

STEP 7: Read the number of times the interest is compounded into the variable n.

STEP 8: calculate compound interest as p * Math.pow(1 + (r / n), n * t).

STEP 9: Display the compound interest.

## Java Source Code

```                                          ```import java.util.Scanner;
public class CInterest
{
public static void main(String args[])
{
double p,r,t,ci;
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Principal amount : ");
System.out.print("Enter the Rate of interest  : ");
r = sc.nextDouble();//Read rate of interset
System.out.print("Enter the number of times the interest is compounded : ");
System.out.print("Enter the Time period : ");
t = sc.nextDouble();//Read the time period
sc.close();
ci =  p * Math.pow(1 + (r / n), n * t);
System.out.print("Compound Interest is: " +ci);
}
}```
```

## OUTPUT

```OUTPUT 1

Enter the Principal amount :2000
Enter the Rate of interest per year : 0.08
Enter the number of times the interest is compounded :12
Enter the time period in year : 5
Compound Interest is:2979.69141660321
```

VIEW ALL
VIEW ALL
VIEW ALL