using only loops , do while , for, if else if statements Write a program that ca
ID: 3535247 • Letter: U
Question
using only loops , do while , for, if else if statements
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 (as a decimal percentage), 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:
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. Display the output in an external file called "savings_account_report.txt"
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
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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.