Calculate Compound Interest

Calculate compound interest given principal, rate, time, and number of times interest is compounded per year.

PythonBeginner

What You'll Learn

  • Using exponentiation with **
  • Implementing the compound interest formula
  • Working with multiple numeric inputs
Python
# Program to calculate compound interest

principal = float(input("Enter principal amount: "))
rate = float(input("Enter annual interest rate (in %): "))
time = float(input("Enter time in years: "))
n = int(input("Enter number of times interest applied per year: "))

amount = principal * (1 + rate / (100 * n)) ** (n * time)
compound_interest = amount - principal

print("Compound Interest is:", compound_interest)
print("Total Amount is:", amount)

Output

Enter principal amount: 1000
Enter annual interest rate (in %): 5
Enter time in years: 2
Enter number of times interest applied per year: 4
Compound Interest is: 104.486... 
Total Amount is: 1104.486...

Compound interest uses the formula:

[ A = P \left(1 + \frac{R}{100n}\right)^{nT} ]

Where:

  • P is principal,
  • R is annual rate in percent,
  • T is time in years,
  • n is the number of times interest is compounded per year,
  • A is the final amount.

We then compute compound interest as A - P. This showcases exponentiation with ** in Python.

Step-by-Step Breakdown

  1. 1Read principal, rate, time, and compounding frequency from the user.
  2. 2Compute the growth factor (1 + rate / (100 * n)).
  3. 3Raise the factor to the power n * time.
  4. 4Multiply by principal to get total amount.
  5. 5Subtract principal to get compound interest.