Let us discuss the formula for compound interest. The formula to calculate compound interest annually is given by:
A = P(1 + R/100) t
Compound Interest = A - P
Where,
- A: amount
- P: principal amount
- R: rate
- T: time span
Using Exponentiation Operator
This example calculates compound interest using the ** (exponent) operator.
P = 1200
R = 5.4
T = 2
A = P * (1 + R/100) ** T
CI = A - P
print("Compound interest:", CI)
Output
Compound interest: 133.0992000000001
Using Built-in pow() Function
Instead of using the ** (exponent) operator, we can also use Python's pow() function.
p = 10000
r = 10.25
t = 5
Amt = p * (pow((1 + r / 100), t))
CI = Amt - p
print("Compound interest:", CI)
Output
Compound interest: 6288.946267774416
Taking Input from User
This method allows the user to enter values at runtime. It uses the same formula but takes user input for principal, rate, and time using the input() function.
p = int(input("Principal amount: "))
r = int(input("Rate of interest: "))
t = int(input("Time in years: "))
Amt = p * (pow((1 + r / 100), t))
CI = Amt - p
print("Compound interest:", CI)
Output
Principal amount: 3000
Rate of interest: 5
Time in years: 3
Compound interest: 472.875
Please refer complete article on Program to find compound interest for more details!
Using for Loop
In this example, we use a for loop to calculate compound interest by repeated multiplications.
p = 1200
r = 5.4
t = 2
Amt = p
for i in range(t):
Amt = Amt * (1 + r / 100)
CI = Amt - p
print("Compound interest:", CI)
Output
Compound interest: 133.0992000000001