//Grace Thummel CS1A Practice Practicum chapter 5, p. 296 #16
//
/************************************************************************
* *
* Calculate Savings Account Balance *
*_______________________________________________________________________*
* This program calculates the total balance in a savings account after *
* a given period of time. It takes into account the monthly deposits, *
* the monthly withdrawls, and the interest earned after each month. *
* *
* Calculation is based on the formulas: *
* monthly_interest = (total_balance + monthly_deposit - *
* monthly_withdrawl) * monthly_interest_rate; *
monthly_balance = monthly_deposit - monthly_withdrawl + *
monthly_interest; *
total_balance += monthly_balance; *
total_interest += monthly_interest; *
*_______________________________________________________________________*
* INPUT *
* annual_interest_percent : Percentage of annual interest *
* starting_balance : Starting balance in the account *
* months : Number of months since account was opened *
* monthly_deposit : Amount deposited in a month *
* float monthly_withdrawl : Amount withdrawn in a month *
* *
* OUTPUT *
* total_deposit : Total amount deposited from all the months *
* total_withdrawl : Total amount deposited from all the months *
* total_balance : Total balance at the end of the time period *
* total_interest : Total earned from interest *
* *
************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
// Initialize Variables
float annual_interest_percent; // Input - Percentage of annual interest
float annual_interest_rate; // Annual interest rate as a decimal
float monthly_interest_rate; // Interest rate per month, annual_interest_rate / 12
float starting_balance; // Input - Starting balance in the account
int months; // Input - Number of months since account was established
float monthly_balance; // Amount added to the total balance per month
float monthly_interest; // Interest earned in one month
float monthly_deposit; // Input - Amount deposited in a month
float monthly_withdrawl; // Input - Amount withdrawn in a month
float total_deposit = 0; // Output/Accumulator - Total amount deposited from all the months
float total_withdrawl = 0; // Output/Accumulator - Total amount deposited from all the months
float total_balance = 0; // Output/Accumulator - Total balance at the end of the time period given
float total_interest = 0; // Output/Accumulator - Total earned from interest
// Get Inputs
cout << "What is the annual interest rate as a percentage?" << endl;
cin >> annual_interest_percent;
cout << "What was the starting balance?" << endl;
cin >> starting_balance;
cout << "How many months have passed since the account was established?" << endl;
cin >> months;
annual_interest_rate = annual_interest_percent / 100;
monthly_interest_rate = annual_interest_rate / 12;
total_balance = starting_balance;
// Monthly Loop
for (int i = 1; i <= months; i++)
{
cout << "How much was deposited month " << i << "?" << endl;
cin >> monthly_deposit;
while (monthly_deposit < 0)
{
cout << "You cannot enter a negative numder for the amount deposited." << endl;
cout << "How much was deposited month " << i << "?" << endl;
cin >> monthly_deposit;
}
total_deposit += monthly_deposit;
cout << "How much was withdrawn month " << i << "?" << endl;
cin >> monthly_withdrawl;
while (monthly_withdrawl < 0)
{
cout << "You cannot enter a negative numder for the amount withdrawn." << endl;
cout << "How much was withdrawn month" << i << "?" << endl;
cin >> monthly_withdrawl;
}
total_withdrawl += monthly_withdrawl;
// Calculate Accumulators
monthly_interest = (total_balance + monthly_deposit - monthly_withdrawl) * monthly_interest_rate;
monthly_balance = monthly_deposit - monthly_withdrawl + monthly_interest;
total_balance += monthly_balance;
total_interest += monthly_interest;
}
// Display Outputs
cout << fixed << setprecision(2);
cout << "The ending balance after " << months << " months is $"
<< total_balance << endl;
cout << "The total amount deposited is $" << total_deposit << endl;
cout << "The total amount withdrawn is $" << total_withdrawl << endl;
cout << "The total amount of interest earned is $" << total_interest << endl;
}