#include #include #include #include void main (void) { double interest_rate, loan_amount, payment_amount, graduation_debt, debt, total_payments; int study_years, payment_years, i; const int max_payment_years = 50; // we want all floating point ouput in dollars and cents cout << setprecision(2) << setiosflags (ios::fixed | ios::showpoint); cout << "Enter interest rate (in %, -ve to stop): "; cin >> interest_rate; while (interest_rate >= 0) { cout << "Enter loan and payment amounts: "; cin >> loan_amount >> payment_amount; // output table header cout << endl << " Study Years Graduation Debt Payment Years Total Paid" << endl << "-----------------------------------------------------------" << endl; for (study_years = 4; study_years <= 10; study_years++) { // compute debt at graduation graduation_debt = 0; for (i = 0; i < study_years; i++) { graduation_debt = (graduation_debt + loan_amount) * (1.0 + (interest_rate / 100.0)); } // output first part of table line cout << setw(6) << study_years << setw(20) << graduation_debt; // compute years to pay debt = graduation_debt; total_payments = 0; payment_years = 0; while ((debt != 0) && (payment_years < max_payment_years)) { debt *= (1 + (interest_rate / 100.0)); if (debt <= payment_amount) { total_payments += debt; debt = 0; } else { total_payments += payment_amount; debt -= payment_amount; } payment_years++; } // output second part of table line if (debt == 0) { // debt paid off cout << setw(15) << payment_years << setw(16) << total_payments << endl; } else if (debt < graduation_debt) { // debt is coming down, though slowly cout << " Calcs aborted at " << max_payment_years << " years" << endl; } else { // the deby will never get paid off cout << " The debt keeps on growing." << endl; } } // the endl leaves a blank line between the table and our prompt message cout << endl << "Enter interest rate (in %, -ve to stop): "; cin >> interest_rate; } }