fork download
  1. //Zachary Abdollahi CS1A Chapter 3, P. 146, #16
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE INTEREST EARNED
  6.  * ____________________________________________________________________________
  7.  *
  8.  * This program calculates the balance in a savings account after one year,
  9.  * assuming there are no deposits other than the original investment.
  10.  *
  11.  * Computation is based on the formula:
  12.  * Amount = Principal x (1 + Rate / T) ^ T
  13.  * _____________________________________________________________________________
  14.  *
  15.  * INPUT
  16.  * principal : Balance in the savings account
  17.  * rate : Interest rate
  18.  * timesCompounded : Number of times interest is compounded during the year
  19.  *
  20.  * OUTPUT
  21.  * interest : Interest earned over the year
  22.  * amount : Amount in savings after one year
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. #include <iomanip>
  27. #include <cmath>
  28. using namespace std;
  29. int main ()
  30. {
  31. float principal; //INPUT - Balance in the savings account
  32. float rate; //INPUT - Interest rate
  33. int timesCompounded; //INPUT - Number of times interest is compounded
  34. float interest; //OUTPUT - Interest earned over the year
  35. float amount; //OUTPUT - Amount in savings after one year
  36.  
  37. //
  38. // Get Input From User
  39. cout << "Enter the principal: ";
  40. cin >> principal;
  41.  
  42. cout << "Enter the interest rate (as a percentage): ";
  43. cin >> rate;
  44.  
  45. cout << "Enter the number of times the interest rate is compounded: ";
  46. cin >> timesCompounded;
  47.  
  48. //
  49. // Compute Amount in Savings and Interest Earned
  50. amount = principal * pow((1 + (rate / 100) / timesCompounded), timesCompounded);
  51. interest = amount - principal;
  52.  
  53. //
  54. // Output Result
  55. cout << fixed << setprecision(2);
  56. cout << endl;
  57. cout << "Interest Rate: " << setprecision(2) << rate << "%" << endl;
  58. cout << "Times Compounded: " << timesCompounded << endl;
  59. cout << "Principal: $ " << setw(7) << principal << endl;
  60. cout << "Interest: $ " << setw(7) << interest << endl;
  61. cout << "Amount in Savings: $ " << setw(7) << amount << endl;
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Enter the principal: Enter the interest rate (as a percentage): Enter the number of times the interest rate is compounded: 
Interest Rate:		 -0.00%
Times Compounded:   21891
Principal:         $    0.00
Interest:          $    0.00
Amount in Savings: $    0.00