Compound Interest
Compound interest (or compounding interest) is the interest calculated on the principal, including all cumulative interest on deposits or loans in the previous period. – Investopedia
Logic
To calculate compound interest we have to use a very simple formula shown below:
Compound interest formula
Where, P = Principle T = Time and R = Rate
To calculate compound interest we have to multiply principle amount with the power of (1 + Rate / 100) with time as an exponent. Remember that in this program we are using pow() function of math.h library to quickly calculate power from given base and exponent.
You may have to use -lm parameter to compile the program. It helps the gcc compiler to include the math library.
gcc CompoundInterest.CPP -lm
Program
#include <iostream> #include <math.h> using namespace std; int main() { float principal, rate, time, result; cout << "Enter principal : "; cin >> principal; cout << endl << "Enter rate : "; cin >> rate; cout << endl << "Enter time (year) : "; cin >> time; // Calculate compound interest result = principal * pow((1 + rate / 100), time); cout << endl << "Compound interest : " << result; return 0; }
Output
Enter principal : 100
Enter rate : 25
Enter time (year) : 6
Compound interest : 381.47