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

Compound Interest: Precision and Compounding Periods in C++

Revisit yearly compound interest in C++ with a focus on accuracy: float vs double, checking pow with a year-by-year loop, and changing the compounding frequency.

BeginnerAdvanced Number ProgramsExample 3 of 13
compound-interest-precision-and-compounding-periods.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

11025 − 10000.

Total Amount = 11025.00

Yearly compounding over two years.

In this one

  • How float precision limits large money calculations
  • How to verify pow with a year-by-year loop
  • How the formula changes for monthly or quarterly compounding
  • Which small type change makes the interest print as zero

How it works

  1. 1

    Read principal, rate, and time.

  2. 2

    Compute the yearly growth factor 1 + rate / 100.

  3. 3

    Raise it to time with pow and multiply by principal.

  4. 4

    Subtract principal to get the interest.

  5. 5

    Print both with fixed and setprecision(2).

Yearly vs monthly for 10000 at 5%, 2 years

Why the frequency matters.

  1. 1.Yearly: 10000 × 1.05² = 11025.00
  2. 2.Monthly: 10000 × (1 + 0.05/12)^24 ≈ 11049.41
  3. 3.Difference ≈ 24.41 in favour of monthly

Edge cases

Rate stored as int

Declare rate as int.

rate / 100 becomes integer division, which is 0 for any rate under 100, so amount equals principal and interest prints 0.00.

Large principal with float

Enter 12345678, 7.5, 30.

float keeps only about 7 significant digits, so the last digits of the amount are not reliable. Use double.

What's going on

Same formula as the beginner page, A = P × (1 + R/100)^T, but this time the question is not "how do I type it" but "can I trust the number it prints, and how would I change it for real banks?"

The code reads principal, rate, and time as float, computes amount = principal * pow((1 + rate / 100), time), then interest = amount - principal, and prints both with two decimals. For 10000 at 5% for 2 years: 1.05² = 1.1025, so amount = 11025.00 and interest = 1025.00.

Precision first. A float holds about 7 significant digits. 11025.00 has 7, so it is fine. Now try principal 12345678 at 7.5% for 30 years. The amount is roughly 108 million, and a float cannot keep the last few rupees exact; the cents you print are partly noise. Switch all five variables to double (about 15–16 digits) and the problem goes away for any realistic loan. The program still compiles either way, which is exactly why this bug survives into homework.

rate / 100 is safe here because rate is a float, so the division is floating-point. If someone changes rate to int, 5 / 100 becomes 0, the factor becomes 1, and the interest prints as 0.00. That one-word change breaks everything silently.

You can check pow with a loop that compounds one year at a time:

double a = principal; for (int y = 0; y < time; y++) a *= 1 + rate / 100;

Year 1: 10500. Year 2: 11025. The loop and pow agree for whole years. The loop cannot do 2.5 years; pow can, because its exponent is a real number.

Compounding frequency is the next real-world change. Banks often compound monthly or quarterly. With n periods per year the formula becomes A = P × (1 + R/(100n))^(nT). Monthly on the same inputs: pow(1 + 5.0/1200, 24) ≈ 1.10494, so about 11049.41 instead of 11025. More frequent compounding pays slightly more. This program uses n = 1.

And one more time, because it costs people marks: ^ is XOR. (1 + rate/100) ^ time with floats does not even compile; with integers it compiles and gives a meaningless number.

Quiz

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

Question 1

Which change makes this program print 0.00 interest for rate 5?

Pick an answer to check it.

Question 2

Monthly compounding on the same yearly rate gives:

Pick an answer to check it.

Questions people ask

How is this page different from Calculate Compound Interest with pow?

The code is the same. That page teaches how to write the formula; this one tests the result for precision and shows how to adapt it to other compounding periods.

What is continuous compounding?

The limit as n grows without end: A = P × e^(rT), written in C++ as principal * exp(rate / 100 * time).

Why print with two decimals?

Money is shown to two places; fixed and setprecision(2) give that format.