fork download
  1. //Jacklyn Isordia CSC5 Chapter 3, P. 146, #17
  2. //
  3. /**************************************************************
  4.  *
  5.  * Calculate Monthly Loan Payments
  6.  * ____________________________________________________________
  7.  * This program calculates the monthly payment for a loan using
  8.  * the formula:
  9.  *
  10.  * Pyament = Rate * (1 + Rate)^N
  11.  * -------------------- * L
  12.  * ((1 + Rate)^N - 1)
  13.  *
  14.  * Rate = monthly interest rate
  15.  * N = number of payment
  16.  * L = loan amount
  17.  * ____________________________________________________________
  18.  * INPUT
  19.  * loanAmount : total amount of loan
  20.  * annualRate : annual interest rate
  21.  * numPayments : number of payments
  22.  *
  23.  * OUTPUT
  24.  * monthlyPayment : payment due of loan
  25.  * annualRate : total amount paid
  26.  * interestPaid : total interest paid
  27.  *
  28.  **************************************************************/
  29. #include <iostream>
  30. #include <iomanip>
  31. #include <cmath>
  32. using namespace std;
  33.  
  34. int main()
  35. {
  36. float loanAmount;
  37. float annualRate;
  38. float monthlyRate;
  39. int numPayments;
  40.  
  41. float monthlyPayment;
  42. float amountPaidBack;
  43. float interestPaid;
  44.  
  45. //
  46. // Input Values
  47. //
  48. cout << "Enter loan amount: ";
  49. cin >> loanAmount;
  50.  
  51. cout << "Enter annual interest rate (percent): ";
  52. cin >> annualRate;
  53.  
  54. cout << "Enter nubmer of payments: ";
  55. cin >> numPayments;
  56.  
  57. //
  58. // Convert Interest Rate
  59. //
  60. monthlyRate = (annualRate / 100) /12;
  61.  
  62. //
  63. // Compute Monthly Payment
  64. //
  65. monthlyPayment =
  66. monthlyRate * pow((1 + monthlyRate), numPayments) /
  67. (pow((1 + monthlyRate), numPayments) - 1) * loanAmount;
  68.  
  69. //
  70. // Compute Totals
  71. //
  72. amountPaidBack = monthlyPayment * numPayments;
  73. interestPaid = amountPaidBack - loanAmount;
  74.  
  75. //
  76. // Output Result
  77. //
  78. cout << fixed << setprecision(2);
  79.  
  80. cout << endl;
  81. cout << "Loan Amount: $" << loanAmount << endl;
  82. cout << "Monthly Interest Rate: " << monthlyRate * 100 << "%" << endl;
  83. cout << "Number of Payments: " << numPayments << endl;
  84. cout << "Monthly Payment: $" << monthlyPayment << endl;
  85. cout << "Amount Paid Back: $" << amountPaidBack << endl;
  86. cout << "Interest Paid: $" << interestPaid << endl;
  87.  
  88. return 0;
  89. }
Success #stdin #stdout 0.01s 5320KB
stdin
10000
12
36
stdout
Enter loan amount: Enter annual interest rate (percent): Enter nubmer of payments: 
Loan Amount:			  $10000.00
Monthly Interest Rate:  1.00%
Number of Payments:	  36
Monthly Payment:	 	  $332.14
Amount Paid Back:		  $11957.16
Interest Paid:		  $1957.16