Python programs · Numbers
Python program to calculate simple and compound interest
Simple interest grows by the same amount every year; compound interest grows on the total so far.
Approach
- Read the principal, the yearly rate and the number of years.
- Simple interest is principal × rate × years ÷ 100.
- Compound interest is principal × (1 + rate ÷ 100) to the power of the years, minus the principal.
The program
principal = float(input()) rate = float(input()) years = int(input()) simple = principal * rate * years / 100 compound = principal * (1 + rate / 100) ** years - principal print("Simple interest:", round(simple, 2)) print("Compound interest:", round(compound, 2))
Sample run
Input
10000 8 3
Output
Simple interest: 2400.0 Compound interest: 2597.12
How the code works
For 10,000 at 8% for 3 years, simple interest is 10,000 × 8 × 3 ÷ 100 = 2,400. Compound interest grows the total each year to 10,800, then 11,664, then 12,597.12, so the interest earned is 2,597.12. round(value, 2) keeps two decimal places, which is usual for money.
Try changing it
- Print the total amount as well as the interest.
- Show the balance at the end of each year with a loop.
Common mistakes
- Using
^for the power. In Python it is**. - Forgetting to divide the rate by 100.