Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Calculate Compound Interest with pow in C++

Compute yearly compound interest in C++ with pow from <cmath>. A beginner lesson on A = P(1 + R/100)^T and why the ^ operator is XOR, not a power.

BeginnerBasic ProgramsExample 11 of 14
calculate-compound-interest-with-pow.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}

Sample Output

Compound Interest = 1025.00

Amount minus principal.

Total Amount = 11025.00

10000 × 1.05².

In this one

  • How to write A = P(1 + R/100)^T with pow
  • Why ^ in C++ is bitwise XOR, not exponent
  • How to get the interest from the final amount
  • Why compound interest beats simple interest over time

How it works

  1. 1

    Include <cmath> so pow is available.

  2. 2

    Read principal, rate, and time.

  3. 3

    Compute 1 + rate / 100 (1.05 for 5%).

  4. 4

    pow raises it to time (1.05^2 = 1.1025) and the result is multiplied by principal.

  5. 5

    interest = amount - principal, then both are printed with two decimals.

Year by year for 10000 at 5%

What pow is doing for you.

  1. 1.Start: 10000
  2. 2.After year 1: 10000 × 1.05 = 10500
  3. 3.After year 2: 10500 × 1.05 = 11025
  4. 4.Interest = 11025 − 10000 = 1025

Edge cases

Using ^ for power

Write amount = principal * (1 + rate/100) ^ time;

With float operands this does not even compile, because ^ only works on integers. With ints it compiles and computes XOR, giving a wrong number with no warning.

Fractional years

Enter 1.5 for time.

pow accepts a non-integer exponent, so the program happily computes 1.05^1.5. Real banks may compound only at whole periods, so check what your problem expects.

What's going on

Compound interest in C++ uses a power: A = P * (1 + R/100)^T. Simple interest is just a multiply. Same inputs as the previous program, different formula.

#include <cmath>

Required for pow. Skip it and pow is an unknown name.

#include <iomanip>

fixed and setprecision(2), same as simple interest.

float principal, rate, time, amount, interest;

Five variables. amount is the future value. interest is what you earned.

amount = principal * pow((1 + rate / 100), time);

Sample: 10000 * (1.05)^2 = 11025. pow takes two doubles; float promotes.

^ is XOR, not exponent. 2 ^ 3 is 1. Don't write (1 + rate/100)^time expecting a power.

interest = amount - principal;

11025 - 10000 = 1025. Printing only amount makes it look like the interest is the whole pile.

Simple interest on the same numbers was 1000. Compound is 1025 because the second year earns interest on 10500, not on 10000.

Some textbooks compound n times per year: P * pow(1 + R/(100*n), n*T). This program uses yearly compounding. Read the formula in the code.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

In C++, what is 2 ^ 3?

Pick an answer to check it.

Question 2

Which header provides pow?

Pick an answer to check it.

Questions people ask

How do I compound monthly?

Use amount = principal * pow(1 + rate / (100 * 12), 12 * time). Each month uses one twelfth of the yearly rate.

Is there a version without pow?

Yes. Multiply by (1 + rate/100) inside a loop that runs time times. The Calculate Compound Interest Five Ways page shows that and other methods.

Why is my answer slightly off in the last digit?

float keeps about 7 significant digits. For large amounts or many years, switch to double.