fork download
  1. //Xinnuo Wang CS1A chapter 3 , P.146, #16
  2. //
  3. /*******************************************************************************
  4. *
  5. * Caculate earned interest
  6. *-------------------------------------------------------------------------------
  7. * This program asks for the principal, the interest rate, and the number of
  8. * timesthe interest is compounded. And caculate the amount of earned interest.
  9. *
  10. * computation is based on the formula:
  11. * Amount = Principal x (1 +Rate/T)^T
  12. * Interest = Amount - Principal
  13. *-------------------------------------------------------------------------------
  14. * INPUT
  15. * principal : Balance in the savings account
  16. * rate : The interest rate in percent
  17. * times : The number of times the interest is compounded during a year
  18. * OUTPUT
  19. * interst : The amount of earned interst
  20. * amount : The amount in savings
  21. *
  22. *******************************************************************************/
  23. #include <iostream>
  24. #include <cmath>
  25. #include <iomanip>
  26. using namespace std;
  27.  
  28. int main()
  29. {
  30. double principal; //INPUT - Balance in the savings account
  31. double rate; //INPUT - The interest rate in percent
  32. int times; //INPUT - The number of times the interest is compounded during a year
  33. double interest; //OUTPUT - The amount of earned interst
  34. double amount; //OUTPUT - The amount in savings
  35. //
  36. //Input data
  37. cout << "Enter balance in the savings account" << endl;
  38. cin >> principal;
  39. cout << "Enter the interest rate" << endl;
  40. cin >> rate;
  41. cout << "Enter the number of times the interest is compounded during a year" << endl;
  42. cin >> times;
  43. //
  44. //Compute the amount
  45. amount = principal * pow(1+rate/100/times,times);
  46. //
  47. //Compute the earned interest
  48. interest = amount - principal;
  49. //
  50. //Output Result
  51. cout <<"Interest Rate: "<<fixed<<setprecision(2)<<setw(10)<<rate<<"%"<<endl;
  52. cout <<"Times Compounded: "<<setw(6)<<times<<endl;
  53. cout <<"Principal: $"<<fixed<<setprecision(2)<<setw(10)<<principal<<endl;
  54. cout <<"Interest: $"<<fixed<<setprecision(2)<<setw(10)<<interest<<endl;
  55. cout <<"Amount in Savings: $"<<fixed<<setprecision(2)<<setw(10)<<amount<<endl;
  56. return 0;
  57. }
Success #stdin #stdout 0s 5316KB
stdin
1234.8 4.4 4
stdout
Enter balance in the savings account
Enter the interest rate
Enter the number of times the interest is compounded during a year
Interest Rate:                  4.40%
Times Compounded:              4
Principal:               $   1234.80
Interest:                $     55.23
Amount in Savings:       $   1290.03