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++Beginner
C++
#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

Calculate Compound Interest in C++

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:

cout << "Enter principal amount: ";
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

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:

cout << fixed << setprecision(2);

  • 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.

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

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++.