// LOANSKED.CPP by J. W. Rider [950302]

 

// Generate loan payment schedule

 

#include <math.h>       // pow()

#include <iostream.h>

#include <iomanip.h>    // setprecision(), setw()

 

void loan_schedule(double principal,        // dollars

                        double interest_rate,    // percentage annually

                      double term_years,

                   double compounding_periods_per_year); // 12 = monthly

 

int main(int argc,char *argv[])

{

    double inprincipal, inrate, interms;

 

    if (argc > 1)

      inprincipal = atof(argv[1]);

    else {

      cout << endl << "Amount of loan:$";

      cin >> inprincipal;

    }

    if ( inprincipal< 0.005) {

      cout << "Bad loan amount!" << endl;

      return 1;

    }

 

    if (argc > 2)

      inrate = atof(argv[2]);

    else {

      cout << "Annual interest rate(%):";

      cin >> inrate;

    }

    if ( inrate < 1) inrate *= 100;

    if ( inrate< 1) {

      cout << "Bad interest rate!" << endl;

      return 1;

    }

 

    if (argc > 3)

      interms = atof(argv[3]);

    else {

      cout << "Number of years:";

      cin >> interms;

    }

    if ( interms< 0.005) {

      cout << "Bad number of years!" << endl;

      return 1;

    }

 

    loan_schedule( inprincipal, inrate, interms, 12);

 

    return 0;

}

 

void loan_schedule(double principal,        // dollars

                      double interest_rate,    // percentage annually

                      double term_years,

                   double compounding_periods_per_year) // 12 = monthly

{

    double irate; // interest rate

    double term;  // number of compounding periods for life of loan

    double paymt; // payment per compounding period.

    int   paymentnum;  // number of payments

    double balance; // running remaining principal to be paid

    double totalpayment; // accumulated payments

    double totalipaid; // accumulated interest paid

    double totalppaid; // accumulated principal paid

 


     // Generate header

 

     cout << endl;

     cout << "Loan Payback Schedule" << endl;

     cout << "on $" << setprecision(2) << principal ;

     cout << " at " << interest_rate << "%" ;

     cout << " annually for " << term_years << " year(s)" << endl ;

 

     if (fabs(compounding_periods_per_year - 1) > 0.0001) {

       cout << "compounded " << compounding_periods_per_year;

       cout << " times per year" << endl;

     }

 

     cout << endl;

     cout << "PAYMENT #  ";

     cout << "BALANCE  ";

     cout << "PAYMENT  ";

     cout << "INTEREST PAID  ";

     cout << "PRINCIPAL PAID  ";

     cout << "REMAINING";

     cout << endl << endl;

 

     // Initialize variables for loop

 

     irate = interest_rate/(100*compounding_periods_per_year);

     term = term_years*compounding_periods_per_year;

     paymt = ceil(100*principal*irate*pow(1+irate,term)

                                      /(pow(1+irate,term)-1))

                  /100;

 

     paymentnum = 0;

     balance = principal;

     totalpayment = 0;

     totalipaid = 0;

     totalppaid = 0;

 

     cout.setf(ios::fixed | ios::showpoint);

     cout.precision(2);

 

     // Report each period's payment

 

     while (balance>0.005) { // need to pay if more 1/2 cent outstanding

       cout << setw(9) << ++paymentnum;

       cout << setw(9)  << balance;

                        if (paymt > balance*(1+irate))

                               paymt = ceil(100*balance*(1+irate)-0.5)/100;

       cout << setw(9)  << paymt;

       totalpayment += paymt;

       cout << setw(15)  << balance*irate ;

       totalipaid += balance*irate;

       cout << setw(16)  << (paymt-balance*irate);

       totalppaid += paymt - balance*irate;

       balance -= paymt - balance*irate;

       cout << setw(11) << balance;

 

       cout << endl;

     }

 

     // Summarize loan payments

 

     cout << endl;

     cout << "Total payments: " << paymentnum << endl;

     cout << "Total paid:     $" << setw(14) << totalpayment << endl;

     cout << "Interest paid:  $" << setw(14) << totalipaid << endl;

     cout << "Principal paid: $" << setw(14) << totalppaid << endl;

}