Compound Interest Formula
A = P \left(1 + \frac{r}{n}\right)^{nt}
where:
A is the final amount
P is the principal investment
r is the annual interest rate (as a decimal)
n is the number of times the interest is compounded per year
t is the time period in years
Python Code
compound_interest.py
def compound_interest(principal, rate, time, compound):
amount = principal * (1 + (rate/compound)) ** (compound*time)
return amount
principal = 1000
rate = 0.03
time = 5
compound = 8
amount = compound_interest(principal, rate, time, compound)
print("Compound interest after {} years: {:.2f}".format(time, amount))
# Output: Compound interest after 5 years: 1161.51