//Ryan Shahriyarpour Carl Argila CS1A Practice Practicum /******************************************************************************* * * Savings Account Balance * * ***************************************************************************** * * Calculates the ending balance of a savings account after a * number of months, given monthly deposits and withdrawals. * * ***************************************************************************** * * INPUT: Annual interest rate (percent), starting balance, * number of months, and each month's deposit and withdrawal. * * * OUTPUT: Ending balance, total deposits, total withdrawals, * and total interest earned. * * * * ****************************************************************************/ #include <iostream> #include <iomanip> using namespace std; int main() { double annualRate, balance; int months; cout << "Enter annual interest rate (%): "; cin >> annualRate; cout << "Enter starting balance: "; cin >> balance; cout << "Enter number of months: "; cin >> months; double monthlyRate = (annualRate / 100) / 12; double totalDeposits = 0, totalWithdrawals = 0, totalInterest = 0; for (int month = 1; month <= months; month++) { cout << "\nMonth " << month << endl; // A) Deposit (no negatives) double deposit; cout << "Enter amount deposited: "; cin >> deposit; while (deposit < 0) { cout << "Cannot be negative. Enter amount deposited: "; cin >> deposit; } balance += deposit; totalDeposits += deposit; // B) Withdrawal (no negatives) double withdrawal; cout << "Enter amount withdrawn: "; cin >> withdrawal; while (withdrawal < 0) { cout << "Cannot be negative. Enter amount withdrawn: "; cin >> withdrawal; } balance -= withdrawal; totalWithdrawals += withdrawal; // Negative balance check if (balance < 0) { cout << "The account has been closed due to a negative balance." << endl; break; } // C) Monthly interest double interest = balance * monthlyRate; balance += interest; totalInterest += interest; } cout << fixed << setprecision(2); cout << "\n--- Summary ---" << endl; cout << "Ending balance: $" << balance << endl; cout << "Total deposits: $" << totalDeposits << endl; cout << "Total withdrawals: $" << totalWithdrawals << endl; cout << "Total interest earned: $" << totalInterest << endl; return 0; }
10 10000 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
Enter annual interest rate (%): Enter starting balance: Enter number of months: Month 1 Enter amount deposited: Enter amount withdrawn: Month 2 Enter amount deposited: Enter amount withdrawn: Month 3 Enter amount deposited: Enter amount withdrawn: Month 4 Enter amount deposited: Enter amount withdrawn: Month 5 Enter amount deposited: Enter amount withdrawn: Month 6 Enter amount deposited: Enter amount withdrawn: Month 7 Enter amount deposited: Enter amount withdrawn: Month 8 Enter amount deposited: Enter amount withdrawn: Month 9 Enter amount deposited: Enter amount withdrawn: Month 10 Enter amount deposited: Enter amount withdrawn: Month 11 Enter amount deposited: Enter amount withdrawn: Month 12 Enter amount deposited: Enter amount withdrawn: Month 13 Enter amount deposited: Enter amount withdrawn: Month 14 Enter amount deposited: Enter amount withdrawn: Month 15 Enter amount deposited: Enter amount withdrawn: Month 16 Enter amount deposited: Enter amount withdrawn: Month 17 Enter amount deposited: Enter amount withdrawn: Month 18 Enter amount deposited: Enter amount withdrawn: Month 19 Enter amount deposited: Enter amount withdrawn: Month 20 Enter amount deposited: Enter amount withdrawn: Month 21 Enter amount deposited: Enter amount withdrawn: Month 22 Enter amount deposited: Enter amount withdrawn: Month 23 Enter amount deposited: Enter amount withdrawn: Month 24 Enter amount deposited: Enter amount withdrawn: Month 25 Enter amount deposited: Enter amount withdrawn: Month 26 Enter amount deposited: Enter amount withdrawn: Month 27 Enter amount deposited: Enter amount withdrawn: Month 28 Enter amount deposited: Enter amount withdrawn: Month 29 Enter amount deposited: Enter amount withdrawn: Month 30 Enter amount deposited: Enter amount withdrawn: Month 31 Enter amount deposited: Enter amount withdrawn: Month 32 Enter amount deposited: Enter amount withdrawn: Month 33 Enter amount deposited: Enter amount withdrawn: Month 34 Enter amount deposited: Enter amount withdrawn: Month 35 Enter amount deposited: Enter amount withdrawn: Month 36 Enter amount deposited: Enter amount withdrawn: --- Summary --- Ending balance: $26120.82 Total deposits: $18000.00 Total withdrawals: $7200.00 Total interest earned: $5320.82