Write a Python program to Calculate Compound Interest with an example. Before we start the program, let me show you the formula behind this Compound Interest:
Future CI = Principal Amount * ( 1 + ROI ) Number of years)
The above calculation is called Future Compound Interest. Because it contains both Principal Amount & CI. To get Compound Interest: Compound Interest = Future CI – Principal Amount.
Python Program to Calculate Compound Interest
This Python program allows Python users to enter the Principal Amount, Rate of Interest, and time period (Number of years). Using those values, it calculates compound Interest using the above-specified formula.
import math
princ_amount = float(input(" Please Enter the Principal Amount : "))
rate_of_int = float(input(" Please Enter the Rate Of Interest : "))
time_period = float(input(" Please Enter Time period in Years : "))
ci_future = princ_amount * (math.pow((1 + rate_of_int / 100), time_period))
compound_int = ci_future - princ_amount
print("Future Compound Interest for Principal Amount {0} = {1}".format(princ_amount, ci_future))
print("Compound Interest for Principal Amount {0} = {1}".format(princ_amount, compound_int))

Calculate compound interest using the exponentiation operator
In this example, we use the exponentiation operator (**) to calculate the compound interest. It is the same as the above, but it replaces the built-in Python math.pow() with the Python exponentiation operator (**).
pa = 15000
ri = 4.5
tm = 5
amt = pa * (1 + ri / 100) ** tm
ci = amt - pa
print('Compound Interest = ', ci)
print('Total Amount After {0} Years = {1}'.format(tm, amt))
Compound Interest = 3692.7290647968694
Total Amount After 5 Years = 18692.72906479687
Use a for loop for annual computation of compound interest
In the above example, we have calculated the compound interest on the principal amount for 5 years. However, there should be an amount breakdown to show customers how the yearly interest accumulates. In such a case, use a for loop to calculate the annual computation of compound interest
amt = 15000
ri = 0.045 #4.5%
tm = 5
for n in range(1, tm + 1):
amt = amt * (1 + ri )
print(f'Year {n}: ${amt:.2f}')
Year 1: $15675.00
Year 2: $16380.37
Year 3: $17117.49
Year 4: $17887.78
Year 5: $18692.73
Also Read