fork download
  1. //Sam Partovi CS1A Chapter 6, P 296, #16
  2. /*******************************************************************************
  3.  * CALCULATE SAVINGS ACCOUNT BALANCE
  4.  *------------------------------------------------------------------------------
  5.  * This program calculates the balance of a savings account after a given period
  6.  * of time, with a specified interest rate and starting balance.
  7.  *
  8.  * Calculations are based on the following formulas:
  9.  *
  10.  * monthlyInterestRate = (interestRatePercent / 100) / 12
  11.  * subtotalBalance = totalDeposit - totalWithdrawal
  12.  * interestYield = subtotalBalance * monthlyInterestRate * timeElapsed
  13.  * finalBalance = subtotalBalance + interestYield
  14.  * -----------------------------------------------------------------------------
  15.  *INPUT
  16.  * interestRatePercent : Annual interest rate (%)
  17.  * initialBalance : Starting account balance ($)
  18.  * timeElapsed : Account age (months)
  19.  * monthlyDeposit : Amount deposited per month ($)
  20.  * monthlyWithdrawal : Amount withdrawn per month ($)
  21.  *
  22.  *OUTPUT
  23.  * monthlyInterestRate : Monthly interest rate (decimal)
  24.  * totalDeposit : Total amount deposited ($)
  25.  * totalWithdrawal : Total amount withdrawn ($)
  26.  * subtotalBalance : Account balance before interest ($)
  27.  * interestYield : Total interest accrued ($)
  28.  * finalBalance : Account balance after interest ($)
  29. *******************************************************************************/
  30. #include <iostream>
  31. #include <iomanip>
  32. using namespace std;
  33.  
  34. int main() {
  35. float interestRatePercent; //INPUT - Annual interest rate (%)
  36. float initialBalance; //INPUT - Starting account balance ($)
  37. int timeElapsed; //INPUT - Account age (months)
  38. float monthlyDeposit; //INPUT - Amount deposited per month ($)
  39. float monthlyWithdrawal; //INPUT - Amount withdrawn per month ($)
  40. float monthlyInterestRate; //OUTPUT - Monthly interest rate (decimal)
  41. float totalDeposit; //OUTPUT - Total amount deposited ($)
  42. float totalWithdrawal; //OUTPUT - Total amount withdrawn ($)
  43. float subtotalBalance; //OUTPUT - Account balance before interest ($)
  44. float interestYield; //OUTPUT - Total interest accrued ($)
  45. float finalBalance; //OUTPUT - Account balance after interest ($)
  46.  
  47. //Prompt for interestRatePercent
  48. cout << "Enter the annual interest rate: ";
  49. cin >> interestRatePercent;
  50.  
  51. //Convert annual interest rate to a decimal monthly interest rate
  52. monthlyInterestRate = (interestRatePercent / 100) / 12;
  53.  
  54. //Prompt for initialBalance
  55. cout << "\nEnter the starting balance: $";
  56. cin >> initialBalance;
  57.  
  58. //Prompt for time elapsed (number of times monthly interest accrues)
  59. cout << "\nEnter the age of the account (months): ";
  60. cin >> timeElapsed;
  61.  
  62. //Iterate through each month based on timeElapsed
  63. for(int count = 1; count <= timeElapsed; count++) {
  64. cout << "\nAmount deposited in month " << count << ": $";
  65. cin >> monthlyDeposit;
  66.  
  67. //Input validation: Deposit can't be negative
  68. if(monthlyDeposit < 0) {
  69. cout << "\nError: amount can not be negative. Please restart the" <<
  70. " program.";
  71. }
  72. else {
  73. cout << "\nAmount withdrawn in month " << count << ": $";
  74. cin >> monthlyWithdrawal;
  75.  
  76. //Input validation: Withdrawal can't be negative
  77. if(monthlyWithdrawal < 0) {
  78. cout << "\nError: amount can not be negative. Please restart the" <<
  79. " program.";
  80. }
  81. else {
  82.  
  83. //Accumulate all deposits and withdrawals
  84. totalDeposit += monthlyDeposit;
  85. totalWithdrawal += monthlyWithdrawal;
  86.  
  87. }
  88. }
  89. }
  90.  
  91. //Format output to 2 decimal places
  92. cout << fixed << setprecision(2);
  93.  
  94. //Calculate subtotalBalance before interest
  95. subtotalBalance = totalDeposit - totalWithdrawal;
  96.  
  97. //Calculate total interest accrued
  98. interestYield = subtotalBalance * monthlyInterestRate * timeElapsed;
  99.  
  100. //Calculate final balance
  101. finalBalance = subtotalBalance + interestYield;
  102.  
  103. //If balance is negative, indicate that the account is closed
  104. if(subtotalBalance < 0 || finalBalance < 0) {
  105. cout << "\nError: The account has been closed due to a negative balance."
  106. << " Please restart the program to try again.";
  107. }
  108.  
  109. else {
  110.  
  111. //Display results of account
  112. cout << "\nYour final account balance is $" << finalBalance << ".";
  113. cout << "\n$" << totalDeposit << " was deposited, and $" << totalWithdrawal <<
  114. " was withdrawn. ";
  115. cout << "\n$" << interestYield << " was earned in interest.";
  116. }
  117.  
  118. return 0;
  119. }
Success #stdin #stdout 0.01s 5268KB
stdin
10
10000.0
36
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
500.0
200.0
stdout
Enter the annual interest rate: 
Enter the starting balance: $
Enter the age of the account (months): 
Amount deposited in month 1: $
Amount withdrawn in month 1: $
Amount deposited in month 2: $
Amount withdrawn in month 2: $
Amount deposited in month 3: $
Amount withdrawn in month 3: $
Amount deposited in month 4: $
Amount withdrawn in month 4: $
Amount deposited in month 5: $
Amount withdrawn in month 5: $
Amount deposited in month 6: $
Amount withdrawn in month 6: $
Amount deposited in month 7: $
Amount withdrawn in month 7: $
Amount deposited in month 8: $
Amount withdrawn in month 8: $
Amount deposited in month 9: $
Amount withdrawn in month 9: $
Amount deposited in month 10: $
Amount withdrawn in month 10: $
Amount deposited in month 11: $
Amount withdrawn in month 11: $
Amount deposited in month 12: $
Amount withdrawn in month 12: $
Amount deposited in month 13: $
Amount withdrawn in month 13: $
Amount deposited in month 14: $
Amount withdrawn in month 14: $
Amount deposited in month 15: $
Amount withdrawn in month 15: $
Amount deposited in month 16: $
Amount withdrawn in month 16: $
Amount deposited in month 17: $
Amount withdrawn in month 17: $
Amount deposited in month 18: $
Amount withdrawn in month 18: $
Amount deposited in month 19: $
Amount withdrawn in month 19: $
Amount deposited in month 20: $
Amount withdrawn in month 20: $
Amount deposited in month 21: $
Amount withdrawn in month 21: $
Amount deposited in month 22: $
Amount withdrawn in month 22: $
Amount deposited in month 23: $
Amount withdrawn in month 23: $
Amount deposited in month 24: $
Amount withdrawn in month 24: $
Amount deposited in month 25: $
Amount withdrawn in month 25: $
Amount deposited in month 26: $
Amount withdrawn in month 26: $
Amount deposited in month 27: $
Amount withdrawn in month 27: $
Amount deposited in month 28: $
Amount withdrawn in month 28: $
Amount deposited in month 29: $
Amount withdrawn in month 29: $
Amount deposited in month 30: $
Amount withdrawn in month 30: $
Amount deposited in month 31: $
Amount withdrawn in month 31: $
Amount deposited in month 32: $
Amount withdrawn in month 32: $
Amount deposited in month 33: $
Amount withdrawn in month 33: $
Amount deposited in month 34: $
Amount withdrawn in month 34: $
Amount deposited in month 35: $
Amount withdrawn in month 35: $
Amount deposited in month 36: $
Amount withdrawn in month 36: $
Your final account balance is $14040.00.
$18000.00 was deposited, and $7200.00 was withdrawn. 
$3240.00 was earned in interest.