Python Program for compound interest

Python Program for compound interest

Compound interest is a fundamental concept in finance and is used to calculate the total amount of interest earned or owed over a period of time, where the interest is periodically added to the principal amount.

The formula to calculate compound interest is:

A=P(1+nr​)nt

Where:

  • A is the future value of the investment/loan, including interest.
  • P is the principal investment amount (initial deposit or loan amount).
  • r is the annual interest rate (in decimal form).
  • n is the number of times interest is compounded per year.
  • t is the number of years the money is invested or borrowed for.

The compound interest is given by A−P.

Python Program for Compound Interest:

def compound_interest(P, r, n, t): """ Calculates the compound interest. Parameters: - P: Principal amount (initial investment or loan amount) - r: Annual interest rate (in decimal form, e.g., 10% is 0.1) - n: Number of times the interest is compounded per year - t: Time in years Returns: - Compound Interest """ # Calculate the future value using the compound interest formula A = P * (1 + r/n) ** (n*t) # Return the compound interest return A - P # Test the function P = 5000 # Principal amount r = 0.05 # Annual interest rate of 5% in decimal form n = 12 # Compounded monthly t = 10 # 10 years CI = compound_interest(P, r, n, t) print(f"Compound Interest for ${P} at {r*100}% over {t} years (compounded {n} times a year) is: ${CI:.2f}") 

In the above example, we are calculating the compound interest for a principal amount of $5000, at an annual interest rate of 5%, compounded monthly, over 10 years. The program will print the resulting compound interest.

This is a basic approach to compound interest, and in real-world applications, additional factors like taxes, additional deposits, or withdrawals might also come into play.


More Tags

database-administration dynamics-crm seconds google-natural-language qstylesheet spring-annotations nodemailer grouping text-formatting sqlplus

More Programming Guides

Other Guides

More Programming Examples