How to Calculate Compound Interest in Python
How to Calculate Compound Interest in Python
How to Calculate Compound Interest in Python
I want to calculate the compound interest for a given principal amount, rate of interest, and time period. How can I do this in Python?
solveurit24@gmail.com Changed status to publish February 16, 2025
The formula for compound interest is A=P(1+rn)nt, where:
- P is the principal amount
- r is the annual interest rate (in decimal)
- n is the number of times interest is compounded per year
- t is the time in years
Code Example:
def compound_interest(principal, rate, time, compounded_yearly):
amount = principal * (1 + rate / compounded_yearly) ** (compounded_yearly * time)
return amount
# Example: $1000 at 5% annual interest, compounded annually for 10 years
print(compound_interest(1000, 0.05, 10, 1))
This function calculates the amount after the given time period and returns the total amount, including the principal and interest.
solveurit24@gmail.com Changed status to publish February 16, 2025