In: Computer Science
using C++
23. Savings Account Balance
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 steps:
A) Ask the user for the total amount deposited into the account
during that month
and add it to the balance. Do not accept negative numbers.
B) Ask the user for the total amount withdrawn from the account
during that
month and subtract it from the balance. Do not accept negative
numbers or
numbers greater than the balance after the deposits for the month
have been
added in.
C) Calculate the interest for that month. The monthly interest rate
is the annual
interest rate divided by 12. Multiply the monthly interest rate by
the average of
that month’s starting and ending balance to get the interest amount
for the
month. This amount should be added to the balance.
After the last iteration, the program should display a report that
includes the following
information:
• starting balance at the beginning of the three-month period
• total deposits made during the three months
• total withdrawals made during the three months
• total interest posted to the account during the three
months
• final balance
test case:
Test Case1: Welcome to Your Bank! What is your starting Balance? $100 What is the annual interest rate?. Please enter whole value. For example 6 for 6% :6 Month #1 Current Balance: $100.00 Please enter total amount of deposits: $2000 Please enter total amount of withdrawals: $200 New Balance: $1905.00 Month #2 Current Balance: $1905.00 Please enter total amount of deposits: $3000 Please enter total amount of withdrawals: $2000 New Balance: $2917.03 Month #3 Current Balance: $2917.03 Please enter total amount of deposits: $4000 Please enter total amount of withdrawals: $2000 New Balance: $4936.61 Start Balance: $100.00 Total Deposits: $9000.00 Total Withdrawals: $4200.00 Total Interest Earned: $36.61 Final Balance: $4936.61
// C++ program to calculate the balance of a savings account at the end of a three month period
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double starting_balance, annual_interest, monthly_interest, ending_balance;
double total_deposits= 0, total_withdrawals = 0, total_interest = 0;
double deposit, withdrawal, interest, balance;
cout<<fixed<<setprecision(2);
cout<<"Welcome to Your Bank!"<<endl;
// input of starting balance
cout<<"What is your starting Balance? $";
cin>>starting_balance;
// input of annual rate of interest
cout<<"What is the annual interest rate?. Please enter whole value. For example 6 for 6% : ";
cin>>annual_interest;
monthly_interest = annual_interest/1200; // get the monthly interest rate
balance = starting_balance;
ending_balance = balance;
// loop to input the deposits and withdrawals for 3-month period
for(int i=0;i<3;i++)
{
cout<<endl<<"Month#"<<(i+1)<<endl;
cout<<"Current Balance: $"<<balance<<endl;
// input of total deposit in the month
cout<<"Please enter total amount of deposits: $";
cin>>deposit;
// validate the deposit to be non-negative, else re-prompt the user until valid
while(deposit < 0)
{
cout<<"Deposit amount cannot be negative"<<endl;
cout<<"Please enter total amount of deposits: $";
cin>>deposit;
}
/// add deposit to ending_balance and total_deposits
total_deposits += deposit;
ending_balance += deposit;
// input of total withdrawal in the month
cout<<"Please enter total amount of withdrawals: $";
cin>>withdrawal;
// validate withdrawal is not negative and not greater than available balance , if invalid re-prompt the user until user enters valid
while(withdrawal < 0 || withdrawal > ending_balance)
{
cout<<"Withdrawal amount cannot be negative or cannot be greater than available balance"<<endl;
cout<<"Please enter total amount of withdrawals: $";
cin>>withdrawal;
}
// add withdrawal to total withdrawals and subtract from ending_balance
total_withdrawals += withdrawal;
ending_balance -= withdrawal;
// calculate interest
interest = monthly_interest*((balance+ending_balance)/2);
// add interest to ending_balance and total_interests
ending_balance += interest;
total_interest += interest;
cout<<"New Balance: $"<<ending_balance<<endl;
balance = ending_balance;
}
// output the report
cout<<endl<<"Start Balance : $"<<starting_balance<<endl;
cout<<"Total Deposits : $"<<total_deposits<<endl;
cout<<"Total Withdrawals : $"<<total_withdrawals<<endl;
cout<<"Total Interest Earned: $"<<total_interest<<endl;
cout<<"Final Balance : $"<<ending_balance<<endl;
return 0;
}
//end of program
Output: