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:
Pis principal,Ris annual rate in percent,Tis time in years,nis the number of times interest is compounded per year,Ais the final amount.
We then compute compound interest as A - P. This showcases exponentiation with ** in Python.
Step-by-Step Breakdown
- 1Read principal, rate, time, and compounding frequency from the user.
- 2Compute the growth factor (1 + rate / (100 * n)).
- 3Raise the factor to the power n * time.
- 4Multiply by principal to get total amount.
- 5Subtract principal to get compound interest.