Skip to content
Laviyx Learn

Try

Python Lab · Experiment 3

Calculate compound interest

PythonWeek 1JNTUH R22 · CS206ES

Aim

To write a Python program that reads the principal, the rate of interest per period and the number of periods, and calculates the final amount and the compound interest.

Algorithm

  1. Read the principal P, the rate of interest R (in percent per period) and the number of periods N.
  2. If any of them is negative, print a message and stop.
  3. Find the amount A = P × (1 + R / 100) to the power N.
  4. Find the compound interest CI = A − P.
  5. Print the amount and the compound interest, rounded to two decimal places.

Program

Python
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest (% per period): "))
periods = int(input("Enter the number of periods: "))

if principal < 0 or rate < 0 or periods < 0:
    print("Principal, rate and periods cannot be negative")
else:
    amount = principal * (1 + rate / 100) ** periods
    interest = amount - principal
    print(f"Amount after {periods} periods = {amount:.2f}")
    print(f"Compound interest = {interest:.2f}")

Output

Whole-number rate

Output 1
Enter the principal amount: 10000
Enter the rate of interest (% per period): 5
Enter the number of periods: 2
Amount after 2 periods = 11025.00
Compound interest = 1025.00

Decimal rate

Output 2
Enter the principal amount: 25000
Enter the rate of interest (% per period): 8.5
Enter the number of periods: 3
Amount after 3 periods = 31932.23
Compound interest = 6932.23

Recorded by running this exact program. Typed input appears after each prompt.

How it works

The interest is compounded once per period. If the rate is given per year but interest is added every month, divide the rate by 12 and multiply the number of years by 12 before using the formula.

input() always returns a string, so the values are converted with float() and int(). Typing a letter instead of a number stops the program with a ValueError; handling that error is covered in the exception-handling experiment.

Check for the first run: 10000 × 1.05 × 1.05 = 11025, so the interest is 1025.

Common mistakes

  • Using ^ for the power. In Python ^ is bitwise XOR. Use **, as in (1 + rate / 100) ** periods.

  • Forgetting to divide the rate by 100. A rate of 5% must be used as 0.05 in the formula.

  • Doing arithmetic on the value from input() without converting it. input() returns a string, so principal * 2 would repeat the text instead of doubling the number.

  • Printing the amount and calling it the interest. Compound interest is the amount minus the principal.

Viva questions

Try to answer each one aloud before you open it.

  • What is the formula for compound interest?

    The amount is A = P × (1 + R/100)ⁿ, where P is the principal, R is the rate per period and n is the number of periods. The compound interest is A − P.

  • How is compound interest different from simple interest?

    Simple interest is calculated only on the original principal. Compound interest is calculated on the principal plus the interest already added, so it grows faster.

  • What does input() return?

    It always returns a string, even if the user types a number. Use int() or float() to convert it.

  • What does the :.2f in the f-string do?

    It formats the number as a float with exactly two digits after the decimal point.

  • Which operator is used for power in Python?

    **. For example 2 ** 3 is 8. The built-in pow(2, 3) gives the same result.

Learn the concepts

All Python Programming Lab experiments →