Calculate Compound Interest
Beginner-friendly C++ program that calculates compound interest using the formula A = P(1 + R/100)^T and prints both interest and total amount.
C++ Calculate Compound Interest Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
float principal, rate, time, amount, interest;
cout << "Enter principal amount: ";
cin >> principal;
cout << "Enter rate of interest (per year): ";
cin >> rate;
cout << "Enter time (in years): ";
cin >> time;
// Compound Interest: A = P(1 + R/100)^T
amount = principal * pow((1 + rate / 100), time);
interest = amount - principal;
cout << fixed << setprecision(2);
cout << "Compound Interest = " << interest << endl;
cout << "Total Amount = " << amount << endl;
return 0;
}Enter principal amount: 10000
Enter rate of interest (per year): 5
Enter time (in years): 2
Compound Interest = 1025.00
Total Amount = 11025.00Understanding Calculate Compound Interest
This program teaches how to calculate compound interest, which is one of the most important concepts in finance and banking. Compound interest grows faster than simple interest because interest is added to the principal repeatedly, and future interest is calculated on the new amount.
---
1. What is Compound Interest?
Compound interest means the interest gets added back to the amount after each time period.
So next time, you earn interest not only on the principal but also on the interest that has already been added.
The formula is:
A = P × (1 + R/100)^T
Where:
Interest = A – P
Example:
If you invest ₹10,000 at 5% for 2 years:
A = 10000 × (1 + 0.05)^2 = 11025
Compound Interest = 11025 – 10000 = 1025
---
2. Header Files Used
Adds cout and cin for input/output.
Used for setprecision() and fixed to format decimal numbers.
Contains pow(), a function used to calculate powers or exponents.
---
3. Declaring Variables
The program declares:
float principal, rate, time, amount, interest;
Floating-point types (float) are used because financial values often have decimal points.
---
4. Taking Input From the User
The program takes three values:
cin >> principal;
cin >> rate;
cin >> time;
The user enters values like:
---
cout << "Enter principal amount: ";5. Applying the Compound Interest Formula
The formula is applied using the pow() function from <cmath>:
amount = principal * pow((1 + rate / 100), time);
Here's what each part does:
So pow(1.05, 2) = 1.1025
Then:
interest = amount - principal;
This subtracts the original money to get only the interest earned.
---
6. Formatting Output
To make the output look clean:
Example:
1025 becomes 1025.00
11025 becomes 11025.00
Very useful for financial programs.
---
cout << fixed << setprecision(2);7. Displaying Results
The program prints:
Compound Interest = 1025.00
Total Amount = 11025.00
This shows both:
---
8. return 0;
This safely ends the program.
---
Summary
This example helps beginners understand formulas, math functions, and proper number formatting in C++.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Calculate Compound Interest
This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.