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
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 + rate / 100is the growth factor for one year, 1.08 for 8%.** timeraises it to the power of the number of years, applying the growth once per year.- Multiplying by
principalgives the final amount, and subtracting the principal leaves only the interest. :.2frounds the money values to two decimal places for printing.
Other ways to write it
Year by year with a loop
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
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.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises