Question

In: Computer Science

Write a program that calculates the balance of a savings account at the end of a...

Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, performing the following:

  1. Ask the user for the amount deposited into the account during the month. (Do not accept negative numbers.) This amount should be added to the balance.
  2. Ask the user for the amount withdrawn from the account during the month. (Do not accept negative numbers.) This amount should be subtracted from the balance.
  3. Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by 12. Multiply the monthly interest rate by the balance, and add the result to the balance.

After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned.

NOTE: If a negative balance is calculated at any point, a message should be displayed indicating the account has been closed, and the loop should terminate.

Solutions

Expert Solution

CODE:

#A function to do necessary calculation on user's account.
def calculate():
#Asking user for annual interest rate, starting balance and months.
annual_interest = float(input("Enter the annual interest rate: "))
start_balance = float(input("Enter the starting balance: "))
months = int(input("Enter the number of months: "))
#Variables to store monthly interest rate, total deposit, total withdrawl and total interest earned.
monthly_interest_rate = annual_interest/12
total_deposit = 0
total_withdrawl = 0
total_interest = 0
#Looping through months one by one.
for i in range(0,months):
#Intitializing amount deposit to -1 to check for negative inputs.
amount_deposit = -1
#It only consider positive input format amount deposit.
while(amount_deposit<0):
amount_deposit = float(input("Amount deposited in " + str(i+1) + " month: "))
#Adding the depoisted amount to account balance.
start_balance += amount_deposit
#Adding to total deposit.
total_deposit += amount_deposit
#Same as amount deposit.
amount_withdrawn = -1
while(amount_withdrawn<0):
amount_withdrawn = float(input("Amount withdrawn in " + str(i+1) + " month: "))
start_balance -= amount_withdrawn
#Checking for negative balance.
if(start_balance<0):
print("Account has been closed")
return
total_withdrawl += amount_withdrawn
#Calculating the monthly interest.
monthly_interest = (start_balance*monthly_interest_rate)/100
#Adding to total interest.
total_interest += monthly_interest
#Adding the interest earned to account balance.
start_balance += monthly_interest
#Printing the required information at the end of the loop.
print("Ending balance: ", round(start_balance))
print("Total deposit: ",total_deposit)
print("Total withdrawl: ",total_withdrawl)
print("Total interest earned: ",round(total_interest))

calculate()
  

CODE SCREENSHOT:

CODE OUTPUT:


Related Solutions

Write a program that calculates the balance of a savings account at the end of a...
Write a program that calculates the balance of a savings account at the end of a three month period. It should ask the user for the starting balance and the annual interest rate. A loop should then iterate once for every month in the period, performing the following: Ask the user for the total amount deposited into the account during that month. Do not accept negative numbers. This amount should be added to the balance. Ask the user for the...
Please use Phyton to write a program: Write a program that calculates and displays the total...
Please use Phyton to write a program: Write a program that calculates and displays the total bill at a restaurant for a couple that is dining. The program should collect from the couple, cost of each meal, and the percentage of the final cost that they would like to tip. The sales tax in the state where the restaurant exists is 7.5%. Display to the user, line by line: Total Cost of Both Meals Sales Tax in dollars Tip in...
If $30,000 is deposited in a savings account at the end of each year and the...
If $30,000 is deposited in a savings account at the end of each year and the account pays interest of 5% compounded annually, what will be the balance of the account at the end of 10 years?
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
Write a program that calculates the mean, median and mode of a given array in the...
Write a program that calculates the mean, median and mode of a given array in the following manner: i) Write three functions mean (), median () and mode () that calculates the mean, median and mode of an array. ii) Using these functions write a program that calculates all the three above mentioned values for two different arrays A and B where A is an array that contains the number of hours spent by a person each day for playing...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $...
Write a C++ program that calculates mileage reimbursement for a salesperson at a rate of $ 0.35 per mile. Your program should interact with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading: 13505.2 Enter ending odometer reading     : 13810.6 You traveled 305.40 miles. At $ 0.35 per mile, your reimbursement is $ 106.89. The values in red color are inputs for the program. The values in blue color are results of expressions.
Suppose that you deposit $871 in a savings account at ProsperityBank at the end of...
Suppose that you deposit $871 in a savings account at Prosperity Bank at the end of each of the next 7 months. You plan to leave these contributions and any interest earned in the account until 7 months are up. The interest rate is 2.47% per month. What is the future value of your account at the end of the holding period? Do not round at intermediate steps in your calculation. Round your final answer to the nearest penny. Do...
In C++, Write a program that calculates pay for either an hourly paid worker or a...
In C++, Write a program that calculates pay for either an hourly paid worker or a salaried worker. Hourly paid workers are paid their hourly pay rate times the number of hours worked. Salaried workers are paid their regular salary plus any bonus they may have earned. The program should declare two structures for the following data: Hourly Paid: HoursWorked HourlyRate Salaried: Salary Bonus The program should also declare a union with two members. Each member should be a structure...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT