Write Python. Understand every error.

Open the editor

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

  1. Read the principal, the yearly rate and the number of years.
  2. Simple interest is principal × rate × years ÷ 100.
  3. 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

Run this program in Py-CRAFT

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

Common mistakes

Learn the ideas

More programs

Share this page: