fork download
  1. // Saliha Babar CS1A Practice Practicum
  2. //
  3. /*****************************************************************************
  4.  * CALCULATE SAVINGS ACCOUNT BALANCE
  5.  * ___________________________________________________________________________
  6.  * This program accepts annual rate, starting balance, and number of months
  7.  * that has passed since the account was established as well as the amount of
  8.  * deposited and withdrawed each month.
  9.  * This program then calculates the ending balance of an account.
  10.  *
  11.  * Formula related to this program
  12.  * Amount of Interest = balance * mothly interest rate
  13.  * Balance = deposited - withdrawn
  14.  * Ending Balance = Initial balance + balance
  15.  * ____________________________________________________________________________
  16.  * INPUT
  17.  * numOfMonths; : number of months
  18.  * annualRate : annual rate
  19.  * monthlyRate; : monthly rate
  20.  * monthlyRateDecimal : monthly rate in decimal
  21.  * initialBalance : starting balance of an account
  22.  * amtDeposited : deposited amount of money each month
  23.  * amtWithdrawn : withdrawed amount of money each month
  24.  *
  25.  * OUTPUT
  26.  * balance : balance of the account
  27.  * totalDeposited; : total deposited amount
  28.  * totalWithdrawn; : total withdrawed amount
  29.  * interestPerMth; : interest earned per month
  30.  * totalInterest : total intrest
  31.  * endingAmt; : ending balance of the account
  32.  *
  33.  
  34.  *
  35.  * ***************************************************************************/
  36. #include <iostream>
  37. #include <iomanip>
  38. using namespace std;
  39.  
  40. int main() {
  41. int numOfMonths; // INPUT - number of months
  42. float annualRate; // INPUT - annual rate
  43. float monthlyRate; // INPUT - monthly rate
  44. float monthlyRateDecimal; // INPUT - monthly rate in decimal
  45. float initialBalance; // INPUT - starting balance of an account
  46. float amtDeposited; // INPUT - deposited amount of money each month
  47. float amtWithdrawn; // INPUT - withdrawed amount of money each month
  48. float balance; // OUTPUT - balance of the account
  49. float totalDeposited; // OUTPUT - total deposited amount
  50. float totalWithdrawn; // OUTPUT - total withdrawed amount
  51. float interestPerMth; // OUTPUT - interest earned per month
  52. float totalInterest; // OUTPUT - total Intrest
  53. float endingAmt; // OUTPUT - ending balance of the account
  54.  
  55. cout << "Enter the annual rate. Eg: enter 10 if the rate is 10%.\n";
  56. cin >> annualRate;
  57. monthlyRate = annualRate / 12;
  58. monthlyRateDecimal = monthlyRate/ 100;
  59.  
  60. cout << "Enter the starting balance of the account\n";
  61. cin >> initialBalance;
  62.  
  63. cout << "Enter number of months passed since the account was established.\n";
  64. cin >> numOfMonths;
  65.  
  66.  
  67. // Set accumulators to 0
  68. balance = initialBalance;
  69. totalDeposited = 0;
  70. totalWithdrawn = 0;
  71. totalInterest = 0;
  72.  
  73.  
  74. for (int i = 1 ; i <= numOfMonths ; i ++)
  75. {
  76. cout << "Enter the amount deposited in the account for month : " << i << endl;
  77. cin >> amtDeposited;
  78.  
  79. while ( amtDeposited < 0 )
  80. {
  81. cout << "Only enter positive number\n";
  82. cin >> amtDeposited;
  83. }
  84. totalDeposited += amtDeposited;
  85.  
  86. cout << "Enter the amount withdrawed in the account for month : " << i << endl;
  87. cin >> amtWithdrawn;
  88.  
  89. while ( amtWithdrawn < 0 )
  90. {
  91. cout << "Only enter positive number\n";
  92. cin >> amtWithdrawn;
  93. }
  94. totalWithdrawn += amtWithdrawn;
  95.  
  96. balance += amtDeposited - amtWithdrawn;
  97. interestPerMth = monthlyRateDecimal * balance;
  98. totalInterest += interestPerMth;
  99.  
  100. balance += interestPerMth;
  101.  
  102. if (balance <= 0)
  103. {
  104. cout << "Balance is negative. The account has been closed.\n";
  105. i = numOfMonths + 1;
  106. }
  107. }
  108.  
  109. // Calculate monthly interest
  110.  
  111. cout << fixed << setprecision(2) << endl;
  112. cout << "Total amounts deposited is $" << totalDeposited << endl;
  113. cout << "Total amounts withdrawls is $" << totalWithdrawn << endl;
  114. cout << "Total interest earned is $" << totalInterest << endl;
  115. cout << "The ending balance is $" << balance << endl;
  116.  
  117. return 0;
  118. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Enter the annual rate. Eg: enter 10 if the rate is 10%.
Enter the starting balance of the account
Enter number of months passed since the account was established.

Total amounts deposited is $0.00
Total amounts withdrawls is $0.00
Total interest earned is $0.00
The ending balance is $0.00