In: Computer Science
Use Python to solve: show all code:
2) For the last assignment you had to write a program that calculated and displayed the end of year balances in a savings account if $1,000 is put in the account at 6% interest for five years.
The program output looked like this:
Balance after year 1 is $ 1060.0
Balance after year 2 is $ 1123.6
Balance after year 3 is $ 1191.02
Balance after year 4 is $ 1262.48
Balance after year 5 is $ 1338.23
Modify your code so that it displays the balances for interest rates from three to five percent inclusive, in one percent intervals. (Hint: Use an outer loop that iterates on the interest rate, and an inner one that iterates on the year.)
Output:
Interest rate of 3 %:
Balance after year 1 is $ 1030.0
Balance after year 2 is $ 1060.9
Balance after year 3 is $ 1092.73
Balance after year 4 is $ 1125.51
Balance after year 5 is $ 1159.27
Interest rate of 4 %:
Balance after year 1 is $ 1040.0
Balance after year 2 is $ 1081.6
Balance after year 3 is $ 1124.86
Balance after year 4 is $ 1169.86
Balance after year 5 is $ 1216.65
Interest rate of 5 %:
Balance after year 1 is $ 1050.0
Balance after year 2 is $ 1102.5
Balance after year 3 is $ 1157.62
Balance after year 4 is $ 1215.51
Balance after year 5 is $ 1276.28
Program:
# set the balance in the account
balanceInAccount = 1000.0
# outer loop that interates for rates on interest
for interest in range(3, 6):
# display header
print("Interest rate of " + str(interest) +
"%:")
balanceWithInterest = balanceInAccount
# inner loop that interates for year
for year in range(1, 6):
# compute the balance
with interest
balanceWithInterest =
balanceWithInterest + (balanceWithInterest * interest/100.00)
# display the
balance
print ("Balance after
year " + str(year) + " is
${0:.2f}".format(balanceWithInterest))
Output:
Program Screenshot: