//Alec White CS1A Chapter 5, P. 296, #16// /******************************************************************************* * * CALCULATE SAVINGS BALANCE * _____________________________________________________________________________ * * This program takes a starting balance of a savings account and the * annual interest rate, and calculates the balance of the account per month * after requesting the amount deposited/withdrawn per month and the accrual * of interest. It then displays the results to the user after the given number * of months has been calculated. * _____________________________________________________________________________ * INPUT * Annual Interest Rate : Annual interest rate offered by the bank * Months Open : Months account has been open * Starting Balance : Starting balance of the account * Amount Deposited : Amount of money deposited from account per month * Amount Withdrawn : Amount of money withdrawn from account per month * * OUTPUT * Ending Balace : Final balance of the account after months open * Total $ Deposited : Total number of $ deposited into the acct after months * open. * Total $ Withdrawn : Total number of $ withdrawn from the account after * months open. ******************************************************************************/ #include <iostream> using namespace std; int main() { float annualInterest; float monthlyInterest; float monthlyInterestAmount; float startingBalance; float amountWithdrawn; float monthlyDeposit = 0; float monthlyWithdrawn = 0; float currentBalance = 0; float totalWithdrawn = 0; float totalDeposited = 0; float totalInterestEarned = 0; int monthsOpen; //prompt user for starting balance, annual interest rate, and months open and echo back cout << "Please enter your starting balance. $"; cin >> startingBalance; if (startingBalance < 0){ cout << "Invalid balance. Please enter positive number." << endl; return 0; } cout << startingBalance << endl; cout << "Please enter the annual interest rate as a decimal (i.e. 10% as .10). "; cin >> annualInterest; cout << annualInterest << endl; cout << "Please enter the amount of months passed since your account was opened. "; cin >> monthsOpen; cout << monthsOpen << endl; // cout << "Starting Balance = " << startingBalance << " Annual Interest = " << annualInterest << " Months Open = " << monthsOpen << endl; //calculate monthly interest and set startingBalance currentBalance = startingBalance; monthlyInterest = annualInterest / 12; //execute a balance update for monthsOpen months for (int i = 0; i < monthsOpen; i++){ //prompt user for amount deposited and add it to balance cout << "How much money was deposited into the account this month? $"; cin >> monthlyDeposit; //echo back to user cout << monthlyDeposit << endl; currentBalance += monthlyDeposit; totalDeposited += monthlyDeposit; //prompt user for amount withdrawn and subtract from balance cout << "How much money was withdrawn from the account this month? $"; cin >> monthlyWithdrawn; //echo back to user cout << monthlyWithdrawn << endl; //input validation if (monthlyWithdrawn < 0){ cout << "Error: Amount withdrawn cannot be a negative number. Please enter a positive number. " << endl; return 0; } //subtract amount withdrawn from current balance and add monthlyWithdrawn to accumulator currentBalance -= monthlyWithdrawn; totalWithdrawn += monthlyWithdrawn; //calculate amount of dollars in interest accrued, add to account balance and add to accumulator monthlyInterestAmount = monthlyInterest * currentBalance; totalInterestEarned += monthlyInterestAmount; currentBalance += monthlyInterestAmount; //end the program if the balance drops below 0 if (currentBalance < 0){ cout << "The bank balance is now negative. Terminating the account. Goodbye! " << endl; return 0; } } //display account information to user after monthsOpen months cout << "Balance information after " << monthsOpen << " months" << endl; cout << "Ending balance: $" << currentBalance << endl; cout << "Total amount deposited: $" << totalDeposited << endl; cout << "Total amount withdrawn: $" << totalWithdrawn << endl; cout << "Total amount accrued in interest: $" << totalInterestEarned << endl; return 0; }
10000 .10 36 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200 500 200
Please enter your starting balance. $10000 Please enter the annual interest rate as a decimal (i.e. 10% as .10). $0.1 Please enter the amount of months passed since your account was opened.36 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 How much money was deposited into the account this month? $500 How much money was withdrawn from the account this month? $200 Balance information after 36 months Ending balance: $26120.8 Total amount deposited: $18000 Total amount withdrawn: $7200 Total amount accrued in interest: $5320.82