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.

BeginnerTopic: Basic Programs
Back

C++ Calculate Compound Interest Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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;
                  }
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

Understanding 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:

P = Principal (starting amount of money)
R = Rate of interest
T = Time in years
A = Final Amount (principal + interest)

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

1.#include <iostream>

Adds cout and cin for input/output.

2.#include <iomanip>

Used for setprecision() and fixed to format decimal numbers.

3.#include <cmath>

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.

principal → starting money
rate → interest rate per year
time → number of years
amount → final amount after interest is applied
interest → compound interest earned

---

4. Taking Input From the User

The program takes three values:

cin >> principal;

cout << "Enter rate of interest (per year): ";

cin >> rate;

cout << "Enter time (in years): ";

cin >> time;

The user enters values like:

principal = 10000
rate = 5
time = 2

---

                  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:

(1 + rate / 100) → Converts the rate percentage into a decimal
Example: 5% becomes 1 + 0.05 = 1.05
pow(base, exponent) → Calculates base^exponent

So pow(1.05, 2) = 1.1025

principal × that value → gives the total amount after compounding

Then:

interest = amount - principal;

This subtracts the original money to get only the interest earned.

---

6. Formatting Output

To make the output look clean:

fixed → displays numbers in normal decimal form
setprecision(2) → shows exactly 2 decimal places

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:

How much interest you earned
The final amount after adding the interest to the principal

---

8. return 0;

This safely ends the program.

---

Summary

Compound interest grows faster than simple interest.
pow() helps compute the exponent part of the formula.
setprecision(2) gives nice, readable financial output.
The program calculates both the compound interest and final amount.

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.

Table of Contents