Compound interest is often called the eighth wonder of the world — and for good reason. Unlike simple interest, which only grows on your original deposit, compound interest earns interest on interest. Over time, that difference becomes enormous.
Here's the formula, how compounding frequency changes the outcome, and complete code examples in JavaScript, Python, Go, and PHP.
The compound interest formula
A = P × (1 + r/n)^(n × t)
Where:
- A = final amount (principal + interest)
- P = principal (initial deposit or loan amount)
- r = annual interest rate (as a decimal — 5% = 0.05)
- n = number of compounding periods per year
- t = time in years
To isolate just the interest earned:
Interest = A − P
Example: €10,000 at 5% for 10 years
With annual compounding (n = 1):
A = 10000 × (1 + 0.05/1)^(1 × 10)
A = 10000 × (1.05)^10
A = 10000 × 1.62889
A ≈ €16,288.95
Interest earned: €6,288.95
With monthly compounding (n = 12):
A = 10000 × (1 + 0.05/12)^(12 × 10)
A = 10000 × (1.004167)^120
A = 10000 × 1.64701
A ≈ €16,470.09
Interest earned: €6,470.09
The difference between annual and monthly compounding: €181.14 — just from compounding more often.
Simple interest vs compound interest
Simple interest only applies to the original principal. Compound interest earns interest on both principal and accumulated interest.
| Year | Simple (5%) | Compound (5% annual) |
|---|---|---|
| 0 | €10,000 | €10,000 |
| 1 | €10,500 | €10,500 |
| 2 | €11,000 | €11,025 |
| 5 | €12,500 | €12,763 |
| 10 | €15,000 | €16,289 |
| 20 | €20,000 | €26,533 |
| 30 | €25,000 | €43,219 |
By year 30, the difference is €18,219 — from the same original €10,000 at the same 5% rate. The only difference is how the interest is calculated.
Compounding frequency comparison
Higher compounding frequency means slightly more interest earned (or paid, on a loan).
| Frequency | n value | A (€10,000 at 5%, 10 years) |
|---|---|---|
| Annually | 1 | €16,288.95 |
| Semi-annually | 2 | €16,386.16 |
| Quarterly | 4 | €16,436.19 |
| Monthly | 12 | €16,470.09 |
| Daily | 365 | €16,486.65 |
| Continuously | ∞ | €16,487.21 |
Continuous compounding uses the formula A = P × e^(r × t) (where e ≈ 2.71828). In practice, the difference between daily and continuous compounding is negligible — under €1 on €10,000 over 10 years.
The Rule of 72
A quick mental shortcut: divide 72 by the annual interest rate to estimate how long it takes money to double.
Years to double ≈ 72 / annual_rate_percent
Examples:
| Rate | Doubles in |
|---|---|
| 2% | 36 years |
| 4% | 18 years |
| 6% | 12 years |
| 8% | 9 years |
| 10% | 7.2 years |
| 12% | 6 years |
At 6%, your €10,000 becomes ~€20,000 in 12 years. At 12%, it doubles in 6 years. The Rule of 72 is accurate within 1–2% for typical interest rates (3–12%).
Compound interest in code
JavaScript
/**
* Calculate compound interest.
* @param {number} principal - Initial amount
* @param {number} annualRate - Annual rate as a decimal (0.05 = 5%)
* @param {number} n - Compounding periods per year (12 = monthly)
* @param {number} t - Time in years
* @returns {{ amount: number, interest: number }}
*/
function compoundInterest(principal, annualRate, n, t) {
const amount = principal * Math.pow(1 + annualRate / n, n * t);
return {
amount: Math.round(amount * 100) / 100,
interest: Math.round((amount - principal) * 100) / 100,
};
}
// Example: €10,000 at 5% monthly for 10 years
const result = compoundInterest(10000, 0.05, 12, 10);
console.log(`Final amount: €${result.amount}`); // €16,470.09
console.log(`Interest earned: €${result.interest}`); // €6,470.09
// Continuous compounding
function continuousCompound(principal, annualRate, t) {
const amount = principal * Math.exp(annualRate * t);
return {
amount: Math.round(amount * 100) / 100,
interest: Math.round((amount - principal) * 100) / 100,
};
}
Python
import math
from decimal import Decimal, ROUND_HALF_UP
def compound_interest(principal: float, annual_rate: float, n: int, t: float) -> dict:
"""
Calculate compound interest.
Args:
principal: Initial amount
annual_rate: Annual rate as a decimal (0.05 = 5%)
n: Compounding periods per year (12 = monthly)
t: Time in years
Returns:
dict with 'amount' and 'interest'
"""
amount = principal * (1 + annual_rate / n) ** (n * t)
interest = amount - principal
return {
"amount": round(amount, 2),
"interest": round(interest, 2),
}
def continuous_compound(principal: float, annual_rate: float, t: float) -> dict:
amount = principal * math.exp(annual_rate * t)
return {
"amount": round(amount, 2),
"interest": round(amount - principal, 2),
}
# Example
result = compound_interest(10_000, 0.05, 12, 10)
print(f"Final amount: €{result['amount']}") # €16470.09
print(f"Interest earned: €{result['interest']}") # €6470.09
# Build a year-by-year growth table
def growth_table(principal: float, annual_rate: float, n: int, years: int):
print(f"{'Year':>5} {'Balance':>12} {'Interest':>12}")
print("-" * 35)
for year in range(1, years + 1):
result = compound_interest(principal, annual_rate, n, year)
print(f"{year:>5} €{result['amount']:>11,.2f} €{result['interest']:>11,.2f}")
growth_table(10_000, 0.05, 12, 10)
Go
package main
import (
"fmt"
"math"
)
// CompoundInterest calculates compound interest.
// annualRate is a decimal (0.05 = 5%), n is periods per year.
func CompoundInterest(principal, annualRate float64, n int, t float64) (amount, interest float64) {
amount = principal * math.Pow(1+annualRate/float64(n), float64(n)*t)
// Round to 2 decimal places
amount = math.Round(amount*100) / 100
interest = math.Round((amount-principal)*100) / 100
return
}
// ContinuousCompound uses A = P * e^(r*t).
func ContinuousCompound(principal, annualRate, t float64) (amount, interest float64) {
amount = principal * math.Exp(annualRate*t)
amount = math.Round(amount*100) / 100
interest = math.Round((amount-principal)*100) / 100
return
}
func main() {
amount, interest := CompoundInterest(10_000, 0.05, 12, 10)
fmt.Printf("Final amount: €%.2f\n", amount) // €16470.09
fmt.Printf("Interest earned: €%.2f\n", interest) // €6470.09
// Year-by-year table
fmt.Printf("\n%-6s %-14s %-12s\n", "Year", "Balance", "Interest")
fmt.Println("-----------------------------------")
for year := 1; year <= 10; year++ {
a, i := CompoundInterest(10_000, 0.05, 12, float64(year))
fmt.Printf("%-6d €%-13.2f €%-11.2f\n", year, a, i)
}
}
PHP
<?php
/**
* Calculate compound interest.
*
* @param float $principal Initial amount
* @param float $annualRate Annual rate as a decimal (0.05 = 5%)
* @param int $n Compounding periods per year
* @param float $t Time in years
* @return array{amount: float, interest: float}
*/
function compoundInterest(float $principal, float $annualRate, int $n, float $t): array
{
$amount = $principal * pow(1 + $annualRate / $n, $n * $t);
$interest = $amount - $principal;
return [
'amount' => round($amount, 2),
'interest' => round($interest, 2),
];
}
function continuousCompound(float $principal, float $annualRate, float $t): array
{
$amount = $principal * exp($annualRate * $t);
return [
'amount' => round($amount, 2),
'interest' => round($amount - $principal, 2),
];
}
// Example
$result = compoundInterest(10_000, 0.05, 12, 10);
echo "Final amount: €{$result['amount']}\n"; // €16470.09
echo "Interest earned: €{$result['interest']}\n"; // €6470.09
// Year-by-year table
printf("%-6s %-14s %-12s\n", "Year", "Balance", "Interest");
echo str_repeat("-", 35) . "\n";
for ($year = 1; $year <= 10; $year++) {
$r = compoundInterest(10_000, 0.05, 12, $year);
printf("%-6d €%-13.2f €%-11.2f\n", $year, $r['amount'], $r['interest']);
}
Quick reference
| Task | What to use |
|---|---|
| Basic compound interest | A = P(1 + r/n)^(nt) |
| Continuous compounding | A = P × e^(rt) |
| Simple interest | A = P × (1 + r × t) |
| Just the interest earned | Interest = A − P |
| Estimate doubling time | 72 / annual_rate_percent |
| Monthly rate from annual | r_monthly = r_annual / 12 |
| Annual effective rate (AER) | (1 + r/n)^n − 1 |
The Annual Effective Rate (AER) is the actual yearly return once compounding is applied. A 5% rate compounded monthly gives an AER of (1 + 0.05/12)^12 − 1 ≈ 5.116%.
6 common mistakes
1. Using the rate as a percentage instead of a decimalr = 5 gives absurd results. Always divide by 100 first: r = 5 / 100 = 0.05.
2. Mixing up n (periods per year) and t (years)n = 12 means monthly compounding. t = 10 means 10 years. Don't swap them — the exponent is n × t, not n + t.
3. Forgetting to subtract the principal for interest earnedA is the total amount including principal. If you want only interest: interest = A − P.
4. Comparing rates without checking compounding frequency
A 5% rate compounded daily is better than 5% compounded annually. Always compare AER (Annual Effective Rate), not the stated rate.
5. Using integer math for money
Floating-point arithmetic accumulates rounding errors. For financial applications, use Decimal in Python, math/big.Float in Go, or bcmath in PHP for precision. Round only at the final output step.
6. Confusing nominal rate and effective rate
Banks often advertise the nominal rate (before compounding). The effective rate (AER) is always higher for sub-annual compounding. Read the fine print.
FAQ
What is the difference between simple and compound interest?
Simple interest applies only to the principal. Compound interest earns interest on the growing balance. Over long periods, compound interest grows much faster.
What does "compounded monthly" mean?
The interest is calculated and added to the balance 12 times per year. Each month's calculation uses the updated balance, so interest accumulates on top of previous interest.
Is compound interest always better?
For savings and investments, yes — more compounding = more return. For loans and debt, no — the same effect works against you. That's why credit card debt (often compounded daily) grows so fast.
What is the Rule of 72?
A mental shortcut: divide 72 by the annual interest rate to estimate the years needed to double your money. At 8%, money doubles in roughly 9 years (72 ÷ 8 = 9).
How do I calculate the interest for just one month?
For a balance B at an annual rate r compounded monthly:monthly_interest = B × (r / 12)
This is what happens inside each step of monthly compounding.
What is APY vs APR?
APR (Annual Percentage Rate) is the nominal rate, usually quoted for loans. APY (Annual Percentage Yield) is the effective rate after compounding, usually quoted for savings. APY is always ≥ APR. When comparing financial products, always compare APY to APY.