Python Program for Compound Interest Calculator
In this tutorial, we will discuss the Python Program for the Compound Interest Calculator.
Before going to the program first, let us understand what is Compound Interest.
Compound Interest:
- Compound interest is calculated using the formula: A=P(1+rn)ntA = P \left(1 + \frac{r}{n}\right)^{nt}, where PP is the principal amount, rr is the annual interest rate, nn is the number of times interest is compounded per year, and tt is the time period in years.
Related: Python Program to find all Prime Numbers in given Range
Program code for Compound Interest Calculator in Python
# Compound Interest Calculator in Python def calculate_compound_interest(principal, rate, times_compounded, years): amount = principal * (1 + rate / times_compounded) ** (times_compounded * years) return amount principal = float(input("Enter the principal amount: ")) rate = float(input("Enter the annual interest rate (in decimal): ")) times_compounded = int(input("Enter the number of times interest is compounded per year: ")) years = float(input("Enter the time period in years: ")) compound_interest = calculate_compound_interest(principal, rate, times_compounded, years) print(f"The compound interest is {compound_interest:.2f}.")
Explanation
- Function Definition: The
calculate_compound_interest
the function takes four parameters:principal
,rate
,times_compounded
, andyears
, and returns the calculated compound interest. - Main Program: The program prompts the user to enter the principal amount, annual interest rate, number of times interest is compounded per year, and time period in years. It then calculates the compound interest using the
calculate_compound_interest
function and prints the result.
Output
- When you run the above program, it will prompt you to enter the principal amount, annual interest rate, number of times interest is compounded per year, and time period in years.
- After entering the values, it will calculate the compound interest and print the result.
Conclusion
- In this tutorial, we learned how to create a compound interest calculator using a Python program.
- Understanding this concept is essential for solving various financial problems and enhancing your programming skills.