Editor

Python Program to Calculate Compound Interest

Beginner · Basics

Compound interest is interest earned on the principal and also on the interest already added, so the money grows faster than with simple interest. The amount after T years is A = P x (1 + R/100) to the power T, and the compound interest is A - P. This program calculates both for interest added once a year.

Program

compound_interest.py
principal = 10000 # amount in rupees
rate = 8 # interest rate in percent per year
time = 3 # time in years
# or: principal = float(input("Enter the principal: ")), and the same for rate and time
amount = principal * (1 + rate / 100) ** time
compound_interest = amount - principal
print(f"Principal: {principal} rupees")
print(f"Rate: {rate}% per year, compounded yearly")
print(f"Time: {time} years")
print(f"Amount: {amount:.2f} rupees")
print(f"Compound interest: {compound_interest:.2f} rupees")
Output
Principal: 10000 rupees
Rate: 8% per year, compounded yearly
Time: 3 years
Amount: 12597.12 rupees
Compound interest: 2597.12 rupees

How it works

  1. 1 + rate / 100 is the growth factor for one year, 1.08 for 8%.
  2. ** time raises it to the power of the number of years, applying the growth once per year.
  3. Multiplying by principal gives the final amount, and subtracting the principal leaves only the interest.
  4. :.2f rounds the money values to two decimal places for printing.

Other ways to write it

Year by year with a loop

Year by year with a loop
principal = 10000 # rupees
rate = 8 # percent per year
balance = principal
for year in range(1, 4):
interest = balance * rate / 100 # interest on the current balance
balance = balance + interest
print(f"Year {year}: interest {interest:.2f}, balance {balance:.2f} rupees")
print(f"Compound interest: {balance - principal:.2f} rupees")
Output
Year 1: interest 800.00, balance 10800.00 rupees
Year 2: interest 864.00, balance 11664.00 rupees
Year 3: interest 933.12, balance 12597.12 rupees
Compound interest: 2597.12 rupees

The interest grows each year (800, then 864, then 933.12) because it is worked out on a balance that already includes earlier interest.

Compounding more than once a year

Compounding more than once a year
principal = 10000 # rupees
rate = 8 # percent per year
time = 3 # years
for name, n in [("yearly", 1), ("half-yearly", 2), ("quarterly", 4), ("monthly", 12)]:
amount = principal * (1 + rate / (100 * n)) ** (n * time)
print(f"Compounded {name}: amount {amount:.2f}, interest {amount - principal:.2f} rupees")
Output
Compounded yearly: amount 12597.12, interest 2597.12 rupees
Compounded half-yearly: amount 12653.19, interest 2653.19 rupees
Compounded quarterly: amount 12682.42, interest 2682.42 rupees
Compounded monthly: amount 12702.37, interest 2702.37 rupees

With n compounding periods a year, the rate per period is rate / n and there are n * time periods, so more frequent compounding earns a little more.

Try it yourself

Find how many years it takes for 10000 rupees at 8% to double, using a while loop.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises