# 1. Introduction

In this tutorial, we are going to write a Java program to calculate Compound Interest. Compound Interest is the interest on a loan or deposit calculated based on both the initial principal and the accumulated interest from previous periods. The formula to calculate compound interest is A = P(1 + r/n)^(nt), where:

– P is the principal amount (initial investment),

– r is the annual nominal interest rate (in decimal form),

– n is the number of times that interest is compounded per unit year,

– t is the time the money is deposited or borrowed in years,

– A is the amount of money accumulated after n years, including interest.

# 2. Program Steps

1. Define a class named *CompoundInterest*.

2. Inside the class, define the *main* method.

3. Inside the *main* method, declare variables for principal (P), rate (r), time (t), and number of times interest applied per time period (n).

4. Apply the compound interest formula and calculate the compound interest.

5. Print out the calculated compound interest.

# 3. Code Program

```
public class CompoundInterest { // Step 1: Define a class named CompoundInterest
public static void main(String[] args) { // Step 2: Define the main method
// Step 3: Declare variables for principal, rate, time and number of times interest applied per time period
double principal = 10000; // Initial amount of money
double rate = 0.05; // Annual interest rate in decimal
double time = 5; // Time in years
int n = 12; // Number of times interest applied per time period
// Step 4: Apply the compound interest formula
double amount = principal * Math.pow(1 + rate/n, n*time);
double compoundInterest = amount - principal;
// Step 5: Print out the calculated compound interest
System.out.println("Compound Interest after " + time + " years: " + compoundInterest);
}
}
```

### Output:

Compound Interest after 5 years: 2823.596930555617

# 4. Step By Step Explanation

– *Step 1:* A class named *CompoundInterest* is defined.

– *Step 2:* The *main* method is defined within the *CompoundInterest* class.

– *Step 3:* Variables for principal (*principal*), rate (*rate*), time (*time*), and number of times interest applied per time period (*n*) are declared and initialized.

– *Step 4:* The compound interest formula is applied to calculate the compound interest. The *Math.pow* method is used to calculate the power of (1 + r/n).

– *Step 5:* The calculated compound interest is printed out.