fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. double Payoff (double loan, double payment, double interest){
  5. double interestAmt, monthlyInterest;
  6. int numMonths, totalMonths, i;
  7.  
  8. totalMonths = loan/payment;
  9. numMonths = 0;
  10.  
  11. cout << "Interest rate per month is ";
  12. monthlyInterest = ((loan * interest) / 12);
  13. cout << monthlyInterest << endl;
  14. monthlyInterest = loan * monthlyInterest;
  15.  
  16. if (loan > payment){
  17. for (numMonths; numMonths < totalMonths; ++numMonths) {
  18. cout << "Mon: " << numMonths << " ";
  19. cout << "Bal: " << loan << " ";
  20. cout << "Int: " << monthlyInterest << " ";
  21. cout << "Pay: " << payment << endl;
  22.  
  23. loan = loan - payment + monthlyInterest;
  24. monthlyInterest = ((loan * interest) / 12);
  25. }
  26. }
  27. return numMonths;
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34. int main() {
  35. double loan, payment, interest;
  36. cout.setf(ios::fixed, ios::floatfield);
  37. cout.precision(2);
  38. cout << "Please enter loan amount: ";
  39. cin >> loan;
  40. cout << loan << endl;
  41. cout << "Please enter monthly payment: ";
  42. cin >> payment;
  43. cout << payment << endl;
  44. cout << "Please enter annual interest: ";
  45. cin >> interest;
  46. cout << interest << endl;
  47.  
  48.  
  49. int numMonths = Payoff(loan, payment, interest);
  50. if (numMonths != -1) {
  51. cout << "It will take " << numMonths;
  52. cout << " months to pay off the loan." << endl;
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0s 16056KB
stdin
1000 100 5 0.42
stdout
Please enter loan amount: 1000.00
Please enter monthly payment: 100.00
Please enter annual interest: 5.00
Interest rate per month is 416.67
Mon: 0 Bal: 1000.00 Int: 416666.67 Pay: 100.00
Mon: 1 Bal: 417566.67 Int: 173986.11 Pay: 100.00
Mon: 2 Bal: 591452.78 Int: 246438.66 Pay: 100.00
Mon: 3 Bal: 837791.44 Int: 349079.76 Pay: 100.00
Mon: 4 Bal: 1186771.20 Int: 494488.00 Pay: 100.00
Mon: 5 Bal: 1681159.20 Int: 700483.00 Pay: 100.00
Mon: 6 Bal: 2381542.20 Int: 992309.25 Pay: 100.00
Mon: 7 Bal: 3373751.45 Int: 1405729.77 Pay: 100.00
Mon: 8 Bal: 4779381.22 Int: 1991408.84 Pay: 100.00
Mon: 9 Bal: 6770690.06 Int: 2821120.86 Pay: 100.00
It will take 10 months to pay off the loan.