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...
Create a Java Program to show a savings account balance. using eclipse IDE This can be...
Create a Java Program to show a savings account balance. using eclipse IDE This can be done in the main() method. Create an int variable named currentBalance and assign it the value of 0. Create an int variable named amountToSaveEachMonth. Prompt "Enter amount to save each month:" and assign the result to the int variable in step 2. Create an int variable name numberOfMonthsToSave. Prompt "Enter the number of months to save:" and store the input value into the variable...
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...
Write a program that calculates the salary of employees. The program should prompt the user to...
Write a program that calculates the salary of employees. The program should prompt the user to enter hourly rate and number of hours of work a day. Then, the program should display the salary daily, bi-weekly (5 days a week), and monthly. Sample program: Enter your hourly rate: >>> 20 Enter how many hours you work a day: >>> 8 Your daily salary is: $160 Your bi-weekly salary is: $1600 Your monthly: $3200
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...
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 multithreaded program that calculates various statistical values for a list of numbers. This program...
Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers 90 81 78 95 79 72 85 The program will report The...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT