fork download
  1. //Maxwell Brewer CS1A Chapter 3, P. 147, #17
  2. //
  3. /***************************************************************
  4.  *
  5.  * CALCULATE MONTHLY PAYMENTS
  6.  * _____________________________________________________________
  7.  *
  8.  * This program will prompt the user for for the loan amount,
  9.  * annual interest rate, and the number of payments.
  10.  * _____________________________________________
  11.  * INPUT
  12.  *
  13.  * Loan amount, annual interest rate, number of payments
  14.  *
  15.  * OUTPUT
  16.  *
  17.  * monthly payment, interest rate, total cost
  18.  *
  19.  ***************************************************************/
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23. #include <cmath>
  24. #include <string>
  25.  
  26. using namespace std;
  27.  
  28. int main() {
  29.  
  30. // Initialization
  31.  
  32. double loanAmt;
  33. double monthlyInt;
  34. double monthlyPay;
  35. double totalPay;
  36. double intPaid;
  37. double annualInt;
  38.  
  39. int numPay;
  40.  
  41. // Display input prompts
  42.  
  43. cout << "Enter the loan amount: \n";
  44. cin >> loanAmt;
  45.  
  46. cout << "Enter the annual interest rate as percent: \n";
  47. cin >> annualInt;
  48.  
  49. cout << "Enter number of payments: \n";
  50. cin >> numPay;
  51.  
  52. monthlyInt = annualInt/(12*100);
  53.  
  54. monthlyPay = (monthlyInt * pow((1+monthlyInt),
  55. numPay)) / ((pow((1+monthlyInt),
  56. numPay) - 1)) * loanAmt;
  57.  
  58. totalPay = numPay * monthlyPay;
  59.  
  60. intPaid = totalPay - loanAmt;
  61.  
  62. // Output
  63.  
  64. cout << fixed << setprecision(2) << endl;
  65.  
  66. cout.width(10);
  67.  
  68. cout << "Loan Amount: $" << loanAmt << endl;
  69. cout << "Monthly Interest Rate: " << monthlyInt << endl;
  70. cout << "Number of Payments: " << numPay << endl;
  71. cout << "Monthly Payment: $" << monthlyPay << endl;
  72. cout << "Amount Paid Back: $" << totalPay << endl;
  73. cout << "Interest Paid: $" << intPaid << endl;
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0s 5284KB
stdin
200000
3
36
stdout
Enter the loan amount: 
Enter the annual interest rate as percent: 
Enter number of payments: 

Loan Amount: $200000.00
Monthly Interest Rate: 0.00
Number of Payments: 36
Monthly Payment: $5816.24
Amount Paid Back: $209384.71
Interest Paid: $9384.71