fork download
  1. //Jeremy Huang CS1A Practice Practicum
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE SAVINGS BALANCE
  6. * ____________________________________________________________
  7. * This program calculates the balance of a savings account at the
  8. * end of a period of time. It takes input for the annual interest
  9. * rate, the starting balance, and the number of months that passed
  10. * since the account was made, and takes the monthly interest and
  11. * adds it to the account for calculation.
  12. * ____________________________________________________________
  13. * INPUT
  14. * annualInterestRate : the annual interest rate
  15. * startingBalance : the starting balance
  16. * months : the months since the account was created
  17. *
  18. * OUTPUT
  19. * balance : total balance
  20. * countDeposit : amount of times deposited
  21. * countWithdrawal : amount of times withdrawn
  22. * totalInterest : total interest earned
  23. *
  24. **************************************************************/
  25.  
  26. #include <iostream>
  27. using namespace std;
  28.  
  29. int main() {
  30. float annualInterestRate;
  31. double startingBalance;
  32. double balance;
  33. double deposit;
  34. double withdrawal;
  35. double totalInterest;
  36. int months;
  37.  
  38. int countDeposit=0;
  39. int countWithdrawal=0;
  40.  
  41. //User Input
  42. cout<<"Enter the annual interest rate (percent): "<<endl;
  43. cin>>annualInterestRate;
  44. cout<<"Enter the starting balance: "<<endl;
  45. cin>>startingBalance;
  46. cout<<"Enter the number of months since the creation of the account: "<<endl;
  47. cin>>months;
  48.  
  49. float monthlyInterestRate = (annualInterestRate/100)/12;
  50.  
  51. //Output Result
  52. for(int i=1; i<=months; i++)
  53. {
  54. cout<<"Enter amount deposited for month "<<i<<": "<<endl;
  55. cin>>deposit;
  56. while (deposit<0)
  57. {
  58. cout<<"Invalid user input, please enter a new amount: "<<endl;
  59. cin>>deposit;
  60. }
  61. balance+=deposit;
  62.  
  63. cout<<"Enter amount withdrawn for month "<<i<<": "<<endl;
  64. cin>>withdrawal;
  65. while (withdrawal<0)
  66. {
  67. cout<<"Invalid user input, please enter a new amount: "<<endl;
  68. cin>>withdrawal;
  69. }
  70. balance-=withdrawal;
  71.  
  72. balance = balance + (balance*monthlyInterestRate);
  73. totalInterest = totalInterest + (balance*monthlyInterestRate);
  74.  
  75. if (deposit>0)
  76. {
  77. countDeposit++;
  78. }
  79.  
  80. if (withdrawal>0)
  81. {
  82. countWithdrawal++;
  83. }
  84.  
  85. if (i==months)
  86. {
  87. cout<<"Ending Balance: "<<balance<<endl;
  88. cout<<"Total amount of deposits: "<<countDeposit<<endl;
  89. cout<<"Total amount of withdrawals: "<<countWithdrawal<<endl;
  90. cout<<"Total interest earned: "<<totalInterest<<endl;
  91. }
  92. }
  93. return 0;
  94. }
Success #stdin #stdout 0.01s 5320KB
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 the annual interest rate: 
Enter the starting balance: 
Enter the number of months since the creation of the account: 
Enter amount deposited for month 1: 
Invalid user input, please enter a new amount: 
Enter amount withdrawn for month 1: 
Enter amount deposited for month 2: 
Enter amount withdrawn for month 2: 
Enter amount deposited for month 3: 
Enter amount withdrawn for month 3: 
Enter amount deposited for month 4: 
Enter amount withdrawn for month 4: 
Enter amount deposited for month 5: 
Enter amount withdrawn for month 5: 
Enter amount deposited for month 6: 
Enter amount withdrawn for month 6: 
Enter amount deposited for month 7: 
Enter amount withdrawn for month 7: 
Enter amount deposited for month 8: 
Enter amount withdrawn for month 8: 
Enter amount deposited for month 9: 
Enter amount withdrawn for month 9: 
Enter amount deposited for month 10: 
Enter amount withdrawn for month 10: 
Enter amount deposited for month 11: 
Enter amount withdrawn for month 11: 
Enter amount deposited for month 12: 
Enter amount withdrawn for month 12: 
Enter amount deposited for month 13: 
Enter amount withdrawn for month 13: 
Enter amount deposited for month 14: 
Enter amount withdrawn for month 14: 
Enter amount deposited for month 15: 
Enter amount withdrawn for month 15: 
Enter amount deposited for month 16: 
Enter amount withdrawn for month 16: 
Enter amount deposited for month 17: 
Enter amount withdrawn for month 17: 
Enter amount deposited for month 18: 
Enter amount withdrawn for month 18: 
Enter amount deposited for month 19: 
Enter amount withdrawn for month 19: 
Enter amount deposited for month 20: 
Enter amount withdrawn for month 20: 
Enter amount deposited for month 21: 
Enter amount withdrawn for month 21: 
Enter amount deposited for month 22: 
Enter amount withdrawn for month 22: 
Enter amount deposited for month 23: 
Enter amount withdrawn for month 23: 
Enter amount deposited for month 24: 
Enter amount withdrawn for month 24: 
Enter amount deposited for month 25: 
Enter amount withdrawn for month 25: 
Enter amount deposited for month 26: 
Enter amount withdrawn for month 26: 
Enter amount deposited for month 27: 
Enter amount withdrawn for month 27: 
Enter amount deposited for month 28: 
Enter amount withdrawn for month 28: 
Enter amount deposited for month 29: 
Enter amount withdrawn for month 29: 
Enter amount deposited for month 30: 
Enter amount withdrawn for month 30: 
Enter amount deposited for month 31: 
Enter amount withdrawn for month 31: 
Enter amount deposited for month 32: 
Enter amount withdrawn for month 32: 
Enter amount deposited for month 33: 
Enter amount withdrawn for month 33: 
Enter amount deposited for month 34: 
Enter amount withdrawn for month 34: 
Enter amount deposited for month 35: 
Enter amount withdrawn for month 35: 
Enter amount deposited for month 36: 
Enter amount withdrawn for month 36: 
Ending Balance: 12639
Total amount of deposits: 36
Total amount of withdrawals: 36
Total interest earned: 1854.33