41.5k views
1 vote
"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: 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. 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. 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. The program should write the following data to an .txt file: ending Balance amount of deposits amount of withdrawal and the amount of interest earned Once the file has been written, display to the user that the 'Report written to Report.txt" c++

1 Answer

6 votes

Answer:

Step-by-step explanation:

Here's an example program in Python that fulfills the requirements:

# Get user inputs

annual_interest_rate = float(input("Enter the annual interest rate: "))

starting_balance = float(input("Enter the starting balance: "))

months = int(input("Enter the number of months since the account was established: "))

# Initialize variables

balance = starting_balance

total_deposits = 0.0

total_withdrawals = 0.0

total_interest_earned = 0.0

# Iterate through each month

for i in range(months):

# Get amount deposited and add to balance

deposit = float(input("Enter the amount deposited for month {}: ".format(i+1)))

while deposit < 0:

deposit = float(input("Amount deposited cannot be negative. Please enter again: "))

balance += deposit

total_deposits += deposit

# Get amount withdrawn and subtract from balance

withdrawal = float(input("Enter the amount withdrawn for month {}: ".format(i+1)))

while withdrawal < 0:

withdrawal = float(input("Amount withdrawn cannot be negative. Please enter again: "))

balance -= withdrawal

total_withdrawals += withdrawal

# Calculate monthly interest and add to balance

monthly_interest_rate = annual_interest_rate / 12

monthly_interest = balance * monthly_interest_rate

balance += monthly_interest

total_interest_earned += monthly_interest

# Write results to file

report_file = open("Report.txt", "w")

report_file.write("Ending Balance: ${:.2f}\\".format(balance))

report_file.write("Total Deposits: ${:.2f}\\".format(total_deposits))

report_file.write("Total Withdrawals: ${:.2f}\\".format(total_withdrawals))

report_file.write("Total Interest Earned: ${:.2f}\\".format(total_interest_earned))

report_file.close()

# Display message to user

print("Report written to Report.txt")

Note that the program validates user inputs to ensure that deposit and withdrawal amounts cannot be negative. The final results are written to a file named "Report.txt". The program prints a message to the user indicating that the report has been written.

User Chad McGrath
by
7.0k points