Program to Create an investment report.
- Jino Shaji
- Jan 19, 2015
- 2 min read
Updated: Jun 9, 2020
“””Program: investment.py
Author: JINO SHAJI
Compute an investment report.
1. The inputs are
starting investment amount
number of years
interest rate (an integer percent)
2. The report is displayed in tabular form with a header.
3. Computations and outputs:
for each year
compute the interest and add it to the investment
print a formatted row of results for that year
4. The total investment and interest earned are also displayed.
“””
# Accept the inputs from the user
startBalance = float(input(“Enter the investment amount: “))
years = int(input(“Enter the number of years: “))
rate = int(input(“Enter the rate as a %: “))
# Convert the rate to a decimal number
rate = rate / 100
# Initialize the accumulator for the interest
totalInterest = 0.0
# Display the header for the table
print(“%4s%18s%10s%16s” % \
(“Year”, “Starting balance”,
“Interest”, “Ending balance”))
# Compute and display the results for each year
for year in range(1, years + 1):
interest = startBalance * rate
endBalance = startBalance + interest
print(“%4d%18.2f%10.2f%16.2f” % \
(year, startBalance, interest, endBalance))
startBalance = endBalance
totalInterest += interest
# Display the totals for the period
print(“Ending balance: $%0.2f” % endBalance)
print(“Total interest earned: $%0.2f” % totalInterest)
Output
Enter the investment amount: 250000
Enter the number of years: 25
Enter the rate as a %: 12.5
Year Starting balance Interest Ending balance
1 250000.00 31250.00 281250.00
2 281250.00 35156.25 316406.25
3 316406.25 39550.78 355957.03
4 355957.03 44494.63 400451.66
5 400451.66 50056.46 450508.12
6 450508.12 56313.51 506821.63
7 506821.63 63352.70 570174.34
8 570174.34 71271.79 641446.13
9 641446.13 80180.77 721626.89
10 721626.89 90203.36 811830.26
11 811830.26 101478.78 913309.04
12 913309.04 114163.63 1027472.67
13 1027472.67 128434.08 1155906.75
14 1155906.75 144488.34 1300395.10
15 1300395.10 162549.39 1462944.48
16 1462944.48 182868.06 1645812.54
17 1645812.54 205726.57 1851539.11
18 1851539.11 231442.39 2082981.50
19 2082981.50 260372.69 2343354.19
20 2343354.19 292919.27 2636273.46
21 2636273.46 329534.18 2965807.64
22 2965807.64 370725.96 3336533.60
23 3336533.60 417066.70 3753600.30
24 3753600.30 469200.04 4222800.34
25 4222800.34 527850.04 4750650.38
Ending balance: $4750650.38
Total interest earned: $4500650.38
Commenti