# C++ Program to Calculate Compound Interest

In this post, we will learn how to calculate compound interest using C++ Programming language.

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: Compound Interest = P x (1 + r/n)nt – P

Where,

• P is the principal amount.
• r is the rate of interest (decimal)
• n is the no. of times the interest is compounded annually
• t is the overall time duration

Since, we are taking n = 1 in the below program, therefore the formula becomes CI = P x (1 + r)t – P.

We will use this formula in the below program to calculate compound interest.

So, without further ado, let’s begin this tutorial.

## C++ Program to Calculate Compound Interest

```// C++ Program to Calculate Compound Interest
#include <iostream>
#include <math.h>
using namespace std;

int main(){
float p, r, t, CI, amount;

cout << "Enter the principal amount: ";
cin >> p;
cout << "Enter the rate of interest: ";
cin >> r;
cout << "Enter the time period in years: ";
cin >> t;

// Calculating compound interest
amount = p * (pow((1 + r/100), t));

CI = amount - p;

// Displaying output
cout << "Compound interest is = " << CI << endl;
return 0;
}
```

Output

``````Enter the principal amount: 50000
Enter the rate of interest: 4
Enter the time period in years: 5
Compound interest is = 10832.6
``````

## How Does This Program Work?

``````    float p, r, t, CI, amount;
``````

In this program, we have declared five integer data type variables named p, r, t, CI and amount.

``````    // Asking for input
cout << "Enter the principal amount: ";
cin >> p;
cout << "Enter the rate of interest: ";
cin >> r;
cout << "Enter the time period in years: ";
cin >> t;
``````

Then, the user is asked to enter the principal amount, rate of interest and time period (in years). The entered values get stored in the p, r, and t named variables respectively.

``````    // Calculating compound interest
amount = p * (pow((1 + r/100), t));

CI = amount - p;
``````

Compound Interest is calculated using the formula: CI = P x (1 + r)t – P.

The pow() function is used to find the value of a base raised to the power of another number. It is defined in the math.h header file.

``````    // Displaying output
cout << "Compound interest is = " << CI << endl;
``````

Finally, the compound interest computed above is displayed on the screen using the cout statement.

## Conclusion

I hope after going through this post, you understand how to calculate compound interest using the C++ Programming language.

If you have any doubts regarding the program, then contact us in the comment section. We will be delighted to assist you.