Cover Image for Python program to find compound interest
195 views

Python program to find compound interest

The compound interest can be calculated using the formula:

[A = P \left(1 + \frac{r}{n}\right)^{nt}]

Where:

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

Here’s a Python program to calculate compound interest:

def calculate_compound_interest(principal, rate, time, comp_per_year):
    # Convert the rate to decimal form
    rate = rate / 100.0

    # Calculate the compound interest
    amount = principal * (1 + rate/comp_per_year)**(comp_per_year*time)
    compound_interest = amount - principal

    return compound_interest

# Input values
principal_amount = float(input("Enter the principal amount: "))
annual_interest_rate = float(input("Enter the annual interest rate (%): "))
investment_time = float(input("Enter the number of years: "))
compounding_frequency = int(input("Enter the number of times interest is compounded per year: "))

# Calculate compound interest
interest = calculate_compound_interest(principal_amount, annual_interest_rate, investment_time, compounding_frequency)

# Print the result
print(f"Compound Interest: ${interest:.2f}")

This program defines a function calculate_compound_interest that takes the principal amount, annual interest rate, time in years, and the number of times interest is compounded per year as inputs. It then calculates and returns the compound interest.

The program prompts the user to enter these values and calculates the compound interest based on the input. Finally, it prints the calculated compound interest.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS