Calculate Compound Interest in C++

C++ Program to Calculate Compound Interest (5 Methods).

BeginnerAdvanced Number ProgramsExample 3 of 13
calculate-compound-interest.cpp
1#include <iostream>
2#include <iomanip>
3#include <cmath>
4using namespace std;
5
6int main() {
7 float principal, rate, time, amount, interest;
8
9 cout << "Enter principal amount: ";
10 cin >> principal;
11
12 cout << "Enter rate of interest (per year): ";
13 cin >> rate;
14
15 cout << "Enter time (in years): ";
16 cin >> time;
17
18 // Compound Interest: A = P(1 + R/100)^T
19 amount = principal * pow((1 + rate / 100), time);
20 interest = amount - principal;
21
22 cout << fixed << setprecision(2);
23 cout << "Compound Interest = " << interest << endl;
24 cout << "Total Amount = " << amount << endl;
25
26 return 0;
27}

Output

Enter principal amount: 10000
Enter rate of interest (per year): 5
Enter time (in years): 2
Compound Interest = 1025.00
Total Amount = 11025.00

What's going on

Compound interest in C++ uses a power: A = P * pow(1 + R/100.0, T). Simple interest is just a multiply.

#include <cmath> is required for pow.

^ is XOR, not exponent. 2 ^ 3 is 1.

Interest is A - P. Printing only A makes it look like the interest is the whole amount.

Some textbooks compound n times per year. This program uses yearly compounding. Read the formula in the code.