To calculate simple and compound interest
Expt. No.04
Page No.8
Simple and compound interest
Aim
To write a Python program that calculates simple interest and compound interest (compounded yearly) for a given principal, rate and time.
Concepts used
Algorithm
- Input principal, rate and time.
- SI = P × R × T ÷ 100.
- Amount with compound interest = P × (1 + R ÷ 100) ** T; CI = amount − P.
- Display SI and CI.
Program
# Simple and compound interest p = float(input("Principal : "))r = float(input("Rate (% a year): "))t = float(input("Time (years) : ")) si = p * r * t / 100amount = p * (1 + r / 100) ** tci = amount - pprint("Simple interest :", round(si, 2))print("Compound interest:", round(ci, 2))
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python simple_compound_interest.py
Principal : 10000
Rate (% a year): 8
Time (years) : 3
Simple interest : 2400.0
Compound interest: 2597.12
✓ Printed by running this program, not typed by hand.
Result
For 10000 at 8% for 3 years the simple interest was 2400.0 and the compound interest 2597.12. Compound interest is higher because each year's interest also earns interest.
Viva voce: what the examiner may ask
01What is the difference between simple and compound interest?
Simple interest is on the principal only; compound interest is also on the interest already added.
02What does ** do?
It raises a number to a power: 1.08 ** 3.
03Why is round() used?
The result has many decimal places; round(ci, 2) keeps two.
Variations for the lab test
- Compound the interest half-yearly (rate ÷ 2, time × 2).
- Display the amount at the end of each year in a loop.
- Find how many years it takes for the amount to double.
! Common mistake Writing
(1 + r / 100 ** t) raises only 100 to the power t; brackets are needed around (1 + r / 100).