fork download
  1. //Grace Thummel CS1A Practice Practicum chapter 5, p. 296 #16
  2. //
  3. /************************************************************************
  4. * *
  5. * Calculate Savings Account Balance *
  6. *_______________________________________________________________________*
  7. * This program calculates the total balance in a savings account after *
  8. * a given period of time. It takes into account the monthly deposits, *
  9. * the monthly withdrawls, and the interest earned after each month. *
  10. * *
  11. * Calculation is based on the formulas: *
  12. * monthly_interest = (total_balance + monthly_deposit - *
  13. * monthly_withdrawl) * monthly_interest_rate; *
  14. monthly_balance = monthly_deposit - monthly_withdrawl + *
  15. monthly_interest; *
  16. total_balance += monthly_balance; *
  17. total_interest += monthly_interest; *
  18. *_______________________________________________________________________*
  19. * INPUT *
  20. * annual_interest_percent : Percentage of annual interest *
  21. * starting_balance : Starting balance in the account *
  22. * months : Number of months since account was opened *
  23. * monthly_deposit : Amount deposited in a month *
  24. * float monthly_withdrawl : Amount withdrawn in a month *
  25. * *
  26. * OUTPUT *
  27. * total_deposit : Total amount deposited from all the months *
  28. * total_withdrawl : Total amount deposited from all the months *
  29. * total_balance : Total balance at the end of the time period *
  30. * total_interest : Total earned from interest *
  31. * *
  32. ************************************************************************/
  33. #include <iostream>
  34. #include <iomanip>
  35. using namespace std;
  36.  
  37. int main ()
  38. {
  39. // Initialize Variables
  40. float annual_interest_percent; // Input - Percentage of annual interest
  41. float annual_interest_rate; // Annual interest rate as a decimal
  42. float monthly_interest_rate; // Interest rate per month, annual_interest_rate / 12
  43. float starting_balance; // Input - Starting balance in the account
  44. int months; // Input - Number of months since account was established
  45. float monthly_balance; // Amount added to the total balance per month
  46. float monthly_interest; // Interest earned in one month
  47. float monthly_deposit; // Input - Amount deposited in a month
  48. float monthly_withdrawl; // Input - Amount withdrawn in a month
  49. float total_deposit = 0; // Output/Accumulator - Total amount deposited from all the months
  50. float total_withdrawl = 0; // Output/Accumulator - Total amount deposited from all the months
  51. float total_balance = 0; // Output/Accumulator - Total balance at the end of the time period given
  52. float total_interest = 0; // Output/Accumulator - Total earned from interest
  53.  
  54.  
  55. // Get Inputs
  56. cout << "What is the annual interest rate as a percentage?" << endl;
  57. cin >> annual_interest_percent;
  58. cout << "What was the starting balance?" << endl;
  59. cin >> starting_balance;
  60. cout << "How many months have passed since the account was established?" << endl;
  61. cin >> months;
  62.  
  63. annual_interest_rate = annual_interest_percent / 100;
  64. monthly_interest_rate = annual_interest_rate / 12;
  65.  
  66. total_balance = starting_balance;
  67.  
  68. // Monthly Loop
  69. for (int i = 1; i <= months; i++)
  70. {
  71. cout << "How much was deposited month " << i << "?" << endl;
  72. cin >> monthly_deposit;
  73. while (monthly_deposit < 0)
  74. {
  75. cout << "You cannot enter a negative numder for the amount deposited." << endl;
  76. cout << "How much was deposited month " << i << "?" << endl;
  77. cin >> monthly_deposit;
  78. }
  79. total_deposit += monthly_deposit;
  80.  
  81. cout << "How much was withdrawn month " << i << "?" << endl;
  82. cin >> monthly_withdrawl;
  83. while (monthly_withdrawl < 0)
  84. {
  85. cout << "You cannot enter a negative numder for the amount withdrawn." << endl;
  86. cout << "How much was withdrawn month" << i << "?" << endl;
  87. cin >> monthly_withdrawl;
  88. }
  89. total_withdrawl += monthly_withdrawl;
  90.  
  91. // Calculate Accumulators
  92. monthly_interest = (total_balance + monthly_deposit - monthly_withdrawl) * monthly_interest_rate;
  93. monthly_balance = monthly_deposit - monthly_withdrawl + monthly_interest;
  94. total_balance += monthly_balance;
  95. total_interest += monthly_interest;
  96. }
  97.  
  98.  
  99. // Display Outputs
  100. cout << fixed << setprecision(2);
  101. cout << "The ending balance after " << months << " months is $"
  102. << total_balance << endl;
  103. cout << "The total amount deposited is $" << total_deposit << endl;
  104. cout << "The total amount withdrawn is $" << total_withdrawl << endl;
  105. cout << "The total amount of interest earned is $" << total_interest << endl;
  106. }
Success #stdin #stdout 0.01s 5548KB
stdin
10
10000
36
-10
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
What is the annual interest rate as a percentage?
What was the starting balance?
How many months have passed since the account was established?
How much was deposited month 1?
You cannot enter a negative numder for the amount deposited.
How much was deposited month 1?
How much was withdrawn month 1?
How much was deposited month 2?
How much was withdrawn month 2?
How much was deposited month 3?
How much was withdrawn month 3?
How much was deposited month 4?
How much was withdrawn month 4?
How much was deposited month 5?
How much was withdrawn month 5?
How much was deposited month 6?
How much was withdrawn month 6?
How much was deposited month 7?
How much was withdrawn month 7?
How much was deposited month 8?
How much was withdrawn month 8?
How much was deposited month 9?
How much was withdrawn month 9?
How much was deposited month 10?
How much was withdrawn month 10?
How much was deposited month 11?
How much was withdrawn month 11?
How much was deposited month 12?
How much was withdrawn month 12?
How much was deposited month 13?
How much was withdrawn month 13?
How much was deposited month 14?
How much was withdrawn month 14?
How much was deposited month 15?
How much was withdrawn month 15?
How much was deposited month 16?
How much was withdrawn month 16?
How much was deposited month 17?
How much was withdrawn month 17?
How much was deposited month 18?
How much was withdrawn month 18?
How much was deposited month 19?
How much was withdrawn month 19?
How much was deposited month 20?
How much was withdrawn month 20?
How much was deposited month 21?
How much was withdrawn month 21?
How much was deposited month 22?
How much was withdrawn month 22?
How much was deposited month 23?
How much was withdrawn month 23?
How much was deposited month 24?
How much was withdrawn month 24?
How much was deposited month 25?
How much was withdrawn month 25?
How much was deposited month 26?
How much was withdrawn month 26?
How much was deposited month 27?
How much was withdrawn month 27?
How much was deposited month 28?
How much was withdrawn month 28?
How much was deposited month 29?
How much was withdrawn month 29?
How much was deposited month 30?
How much was withdrawn month 30?
How much was deposited month 31?
How much was withdrawn month 31?
How much was deposited month 32?
How much was withdrawn month 32?
How much was deposited month 33?
How much was withdrawn month 33?
How much was deposited month 34?
How much was withdrawn month 34?
How much was deposited month 35?
How much was withdrawn month 35?
How much was deposited month 36?
How much was withdrawn month 36?
The ending balance after 36 months is $26120.82
The total amount deposited is $18000.00
The total amount withdrawn is $7200.00
The total amount of interest earned is $5320.82