Python: Compute the future value of a specified principal amount, rate of interest, and a number of years
Future Value Calculator
Write a Python program to compute the future value of a specified principal amount, rate of interest, and number of years.
The formula for future value with compound interest is FV = P(1 + r/n)^nt.
FV = the future value;
P = the principal;
r = the annual interest rate expressed as a decimal;
n = the number of times interest is paid each year;
t = time in years.
Test Data : amt = 10000, int = 3.5, years = 7
Expected Output : 12722.79
Sample Solution:
Python Code:
# Define the principal amount (initial investment).
amt = 10000
# Define the annual interest rate as a percentage.
int = 3.5
# Define the number of years.
years = 7
# Calculate the future value of the investment using the compound interest formula.
future_value = amt * ((1 + (0.01 * int)) ** years)
# Round the future value to two decimal places and print it.
print(round(future_value, 2))
Sample Output:
12722.79
Explanation:
The said Python code calculates the future value of an investment of $10000, with an interest rate of 3.5% and a number of years of 7.
- A variable "amt" is defined and assigned the value of 10000.
- Another variable "int" is defined and assigned the value of 3.5.
- A variable "years" is defined and assigned the value of 7.
- A variable "future_value" is defined and assigned the value of the calculation of the future value of the investment. The calculation is done by the mathematical formula amt*((1+(0.01*int)) ** years).
- The final value is then rounded off to 2 decimal places and printed using the round() and print() function.
The final output will be 12722.79 which is the future value of the investment after 7 years with an interest rate of 3.5%.
Python Code Editor:
Previous: Write a Python program to solve (x + y) * (x + y).
Next: Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics