Hi need a help for the solution of this solution: Write a C++ program that calcu
ID: 3628645 • Letter: H
Question
Hi need a help for the solution of this solution:Write a C++ program that calculates the balance of a savings account at the end of a period of time. It should ask to the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. Do not accept number of months lower than 1. 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 (input validation). The 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 (input validation). The amount should be subtracted from the balance.
(3) Calculate the monthly interest. The monthly interest rate is the annual interest rate divided by twelve. Hint: monthly interest rate = (annual interest rate percentage /100) / 12 To get the monthly interest multiply the monthly interest rate by the balance, and add the result to the balance.
After the last iteration, the program should display the starting balance, ending balance, the total deposits, the total amount of withdrawals, and the total interest earned.
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.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{double balance,start, rate,interest,deposit,withdrawal,totd=0,totw=0,toti=0;
int months,i;
cout<<"Enter starting balance: ";
cin>>balance;
start=balance;
cout<<"Enter annual interest rate: ";
cin>>rate;
rate=rate/100./12.;
cout<<"enter months: ";
cin>>months;
while(months<1)
{cout<<"must be greater than 0 ";
cout<<"enter months: ";
cin>>months;
}
for(i=1;i<=months;i++)
{cout<<"enter deposit for month "<<i<<": ";
cin>>deposit;
while(deposit<0)
{cout<<"must not be negative ";
cout<<"enter deposit for month "<<i<<": ";
cin>>deposit;
}
cout<<"enter withdrawal for month "<<i<<": ";
cin>>withdrawal;
while(withdrawal<0)
{cout<<"must not be negative ";
cout<<"enter withdrawal for month "<<i<<": ";
cin>>withdrawal;
}
balance=balance+deposit-withdrawal;
if(balance<0)
{cout<<"balance<0 ("<<balance<<") account closed ";
break;
}
interest=rate*balance;
balance+=interest;
totw+=withdrawal;
toti+=interest;
totd+=deposit;
}
cout<<"Starting balance: $"<<setprecision(2)<<fixed<<start<<endl;
cout<<"Ending balance: $"<<setprecision(2)<<fixed<<balance<<endl;
cout<<"Total deposits: $"<<setprecision(2)<<fixed<<totd<<endl;
cout<<"Total withdrawals:$"<<setprecision(2)<<fixed<<totw<<endl;
cout<<"Total interest: $"<<setprecision(2)<<fixed<<toti<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.