fork download
  1.  
  2. //Ryan Shahriyarpour Carl Argila CS1A Practice Practicum
  3. /*******************************************************************************
  4.  *
  5.  * Savings Account Balance
  6.  *
  7.  * *****************************************************************************
  8.  *
  9.  * Calculates the ending balance of a savings account after a
  10.  * number of months, given monthly deposits and withdrawals.
  11.  *
  12.  * *****************************************************************************
  13.  *
  14.  * INPUT: Annual interest rate (percent), starting balance,
  15.  * number of months, and each month's deposit and withdrawal.
  16.  *
  17.  *
  18.  * OUTPUT: Ending balance, total deposits, total withdrawals,
  19.  * and total interest earned.
  20.  *
  21.  *
  22.  *
  23.  * ****************************************************************************/
  24.  
  25. #include <iostream>
  26. #include <iomanip>
  27. using namespace std;
  28.  
  29. int main() {
  30. double annualRate, balance;
  31. int months;
  32.  
  33. cout << "Enter annual interest rate (%): ";
  34. cin >> annualRate;
  35. cout << "Enter starting balance: ";
  36. cin >> balance;
  37. cout << "Enter number of months: ";
  38. cin >> months;
  39.  
  40. double monthlyRate = (annualRate / 100) / 12;
  41. double totalDeposits = 0, totalWithdrawals = 0, totalInterest = 0;
  42.  
  43. for (int month = 1; month <= months; month++) {
  44. cout << "\nMonth " << month << endl;
  45.  
  46. // A) Deposit (no negatives)
  47. double deposit;
  48. cout << "Enter amount deposited: ";
  49. cin >> deposit;
  50. while (deposit < 0) {
  51. cout << "Cannot be negative. Enter amount deposited: ";
  52. cin >> deposit;
  53. }
  54. balance += deposit;
  55. totalDeposits += deposit;
  56.  
  57. // B) Withdrawal (no negatives)
  58. double withdrawal;
  59. cout << "Enter amount withdrawn: ";
  60. cin >> withdrawal;
  61. while (withdrawal < 0) {
  62. cout << "Cannot be negative. Enter amount withdrawn: ";
  63. cin >> withdrawal;
  64. }
  65. balance -= withdrawal;
  66. totalWithdrawals += withdrawal;
  67.  
  68. // Negative balance check
  69. if (balance < 0) {
  70. cout << "The account has been closed due to a negative balance." << endl;
  71. break;
  72. }
  73.  
  74. // C) Monthly interest
  75. double interest = balance * monthlyRate;
  76. balance += interest;
  77. totalInterest += interest;
  78. }
  79.  
  80. cout << fixed << setprecision(2);
  81. cout << "\n--- Summary ---" << endl;
  82. cout << "Ending balance: $" << balance << endl;
  83. cout << "Total deposits: $" << totalDeposits << endl;
  84. cout << "Total withdrawals: $" << totalWithdrawals << endl;
  85. cout << "Total interest earned: $" << totalInterest << endl;
  86.  
  87. return 0;
  88. }
  89.  
Success #stdin #stdout 0s 5316KB
stdin
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
stdout
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