// THIS PROGRAM CALCULATE THE NUMBER OF MONTHS NEEDED TO PAYOFF A LOAN // AND THE AMOUNT OF INTEREST PAID ON THE LOAN. // // Written by Hosung Leo Kim #include #include using namespace std; int main() { float loan = 1000; float monthlyPayment = 50; float monthlyRate = 0.015; float debt; float towardLoan; float monthlyInterest; float monthlyInterestSum = 0; int month = 0; cout.setf( ios::fixed ); cout.setf( ios::showpoint ); cout.precision( 2 ); cout << "The total amount of the loan? "; cin >> loan; cout << "The monthly payment? "; cin >> monthlyPayment; cout << "The monthly interest rate? "; cin >> monthlyRate; // THE MONTHLY PAYMENT SHOULD LARGE ENOUGH TO PAY OFF // THE MONTHLY INTEREST AND SOME OF THE DEBT. if ( ( loan * monthlyRate ) >= monthlyPayment ) { cout << "The monthly payment is NOT large enough to payback the loan. Bye." << endl; exit(1); } debt = loan; while ( true ) { month++; cout << "Month #" << month << ": "; monthlyInterest = debt * monthlyRate; cout << "monthlyInterest = " << monthlyInterest << ", "; monthlyInterestSum += monthlyInterest; // WHEN THE AMOUNT OF MONEY NEEDED TO PAYOFF IS LESS THAN // THE NORMAL MONTHLY PAYMENT AMOUNT, EXIT THE LOOP. if ( ( debt + monthlyInterest ) < monthlyPayment ) { cout << "the last payment = " << ( debt + monthlyInterest ) << endl; break; } towardLoan = (monthlyPayment - monthlyInterest); cout << "towardLoan = " << towardLoan << ", "; debt = debt - towardLoan; cout << "debt = " << debt << endl; } // PRINT OUT THE SUMMARY cout << "The number of months needed to payoff the loan: " << month << endl; cout << "The amount of interest paid on the loan: " << monthlyInterestSum << endl; return 0; } /* /homes/home30/hkim16/107/lab5> a.out The total amount of the loan? 1000 The monthly payment? 50 The monthly interest rate? 0.015 Month #1: monthlyInterest = 15.00, towardLoan = 35.00, debt = 965.00 Month #2: monthlyInterest = 14.47, towardLoan = 35.53, debt = 929.47 Month #3: monthlyInterest = 13.94, towardLoan = 36.06, debt = 893.42 Month #4: monthlyInterest = 13.40, towardLoan = 36.60, debt = 856.82 Month #5: monthlyInterest = 12.85, towardLoan = 37.15, debt = 819.67 Month #6: monthlyInterest = 12.30, towardLoan = 37.70, debt = 781.97 Month #7: monthlyInterest = 11.73, towardLoan = 38.27, debt = 743.70 Month #8: monthlyInterest = 11.16, towardLoan = 38.84, debt = 704.85 Month #9: monthlyInterest = 10.57, towardLoan = 39.43, debt = 665.42 Month #10: monthlyInterest = 9.98, towardLoan = 40.02, debt = 625.40 Month #11: monthlyInterest = 9.38, towardLoan = 40.62, debt = 584.79 Month #12: monthlyInterest = 8.77, towardLoan = 41.23, debt = 543.56 Month #13: monthlyInterest = 8.15, towardLoan = 41.85, debt = 501.71 Month #14: monthlyInterest = 7.53, towardLoan = 42.47, debt = 459.24 Month #15: monthlyInterest = 6.89, towardLoan = 43.11, debt = 416.13 Month #16: monthlyInterest = 6.24, towardLoan = 43.76, debt = 372.37 Month #17: monthlyInterest = 5.59, towardLoan = 44.41, debt = 327.95 Month #18: monthlyInterest = 4.92, towardLoan = 45.08, debt = 282.87 Month #19: monthlyInterest = 4.24, towardLoan = 45.76, debt = 237.11 Month #20: monthlyInterest = 3.56, towardLoan = 46.44, debt = 190.67 Month #21: monthlyInterest = 2.86, towardLoan = 47.14, debt = 143.53 Month #22: monthlyInterest = 2.15, towardLoan = 47.85, debt = 95.68 Month #23: monthlyInterest = 1.44, towardLoan = 48.56, debt = 47.12 Month #24: monthlyInterest = 0.71, the last payment = 47.83 The number of months needed to payoff the loan: 24 The amount of interest paid on the loan: 197.83 /homes/home30/hkim16/107/lab5> */