//Sam Partovi CS1A Chapter 6, P 296, #16
/*******************************************************************************
* CALCULATE SAVINGS ACCOUNT BALANCE
*------------------------------------------------------------------------------
* This program calculates the balance of a savings account after a given period
* of time, with a specified interest rate and starting balance.
*
* Calculations are based on the following formulas:
*
* monthlyInterestRate = (interestRatePercent / 100) / 12
* subtotalBalance = totalDeposit - totalWithdrawal
* interestYield = subtotalBalance * monthlyInterestRate * timeElapsed
* finalBalance = subtotalBalance + interestYield
* -----------------------------------------------------------------------------
*INPUT
* interestRatePercent : Annual interest rate (%)
* initialBalance : Starting account balance ($)
* timeElapsed : Account age (months)
* monthlyDeposit : Amount deposited per month ($)
* monthlyWithdrawal : Amount withdrawn per month ($)
*
*OUTPUT
* monthlyInterestRate : Monthly interest rate (decimal)
* totalDeposit : Total amount deposited ($)
* totalWithdrawal : Total amount withdrawn ($)
* subtotalBalance : Account balance before interest ($)
* interestYield : Total interest accrued ($)
* finalBalance : Account balance after interest ($)
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float interestRatePercent; //INPUT - Annual interest rate (%)
float initialBalance; //INPUT - Starting account balance ($)
int timeElapsed; //INPUT - Account age (months)
float monthlyDeposit; //INPUT - Amount deposited per month ($)
float monthlyWithdrawal; //INPUT - Amount withdrawn per month ($)
float monthlyInterestRate; //OUTPUT - Monthly interest rate (decimal)
float totalDeposit; //OUTPUT - Total amount deposited ($)
float totalWithdrawal; //OUTPUT - Total amount withdrawn ($)
float subtotalBalance; //OUTPUT - Account balance before interest ($)
float interestYield; //OUTPUT - Total interest accrued ($)
float finalBalance; //OUTPUT - Account balance after interest ($)
//Prompt for interestRatePercent
cout << "Enter the annual interest rate: ";
cin >> interestRatePercent;
//Convert annual interest rate to a decimal monthly interest rate
monthlyInterestRate = (interestRatePercent / 100) / 12;
//Prompt for initialBalance
cout << "\nEnter the starting balance: $";
cin >> initialBalance;
//Prompt for time elapsed (number of times monthly interest accrues)
cout << "\nEnter the age of the account (months): ";
cin >> timeElapsed;
//Iterate through each month based on timeElapsed
for(int count = 1; count <= timeElapsed; count++) {
cout << "\nAmount deposited in month " << count << ": $";
cin >> monthlyDeposit;
//Input validation: Deposit can't be negative
if(monthlyDeposit < 0) {
cout << "\nError: amount can not be negative. Please restart the" <<
" program.";
}
else {
cout << "\nAmount withdrawn in month " << count << ": $";
cin >> monthlyWithdrawal;
//Input validation: Withdrawal can't be negative
if(monthlyWithdrawal < 0) {
cout << "\nError: amount can not be negative. Please restart the" <<
" program.";
}
else {
//Accumulate all deposits and withdrawals
totalDeposit += monthlyDeposit;
totalWithdrawal += monthlyWithdrawal;
}
}
}
//Format output to 2 decimal places
cout << fixed << setprecision(2);
//Calculate subtotalBalance before interest
subtotalBalance = totalDeposit - totalWithdrawal;
//Calculate total interest accrued
interestYield = subtotalBalance * monthlyInterestRate * timeElapsed;
//Calculate final balance
finalBalance = subtotalBalance + interestYield;
//If balance is negative, indicate that the account is closed
if(subtotalBalance < 0 || finalBalance < 0) {
cout << "\nError: The account has been closed due to a negative balance."
<< " Please restart the program to try again.";
}
else {
//Display results of account
cout << "\nYour final account balance is $" << finalBalance << ".";
cout << "\n$" << totalDeposit << " was deposited, and $" << totalWithdrawal <<
" was withdrawn. ";
cout << "\n$" << interestYield << " was earned in interest.";
}
return 0;
}