Program For Making A Table With Double Nested For-Loops


#include <iostream.h>
#include <math.h>
#include <iomanip.h>

int main()
{
  double basePayment = 7.50, payment,  pmtIncrement  = 0.25,
            baseRate =  0.09,    rate,  rateIncrement = 0.01 ,
           
           temp1, temp2, months, interest ;

   int numPmtIncrs = 50, numRateIncrs = 3;

   cout << endl << endl ;
   cout << "PAYING OFF A LOAN IN MONTHLY INSTALLMENTS" << endl << endl ;
   cout << "Approximate Total Interest Paid And Years To Pay Off" << endl ; 
   cout << "For Given Yearly Interest Rate And Size Of Monthly Payment" ;
   cout << endl << endl << endl ;
   cout << "(\"Infinity\" means the monthly payment is too low." << endl ;
   cout << "In these cases the loan will never be repaid." << endl;
   cout << "Warning: the first entry under the last" << endl ;
   cout << "\"infinity\" in a column can be very " << endl ;
   cout << "inaccurate.)" << endl << endl ;
   cout << setprecision(1) 
        << setiosflags(ios::showpoint | ios::fixed) ;

   cout << setw(30) << "rates ----> " << endl;
   cout << setw(10) << "payment" ;

   double titleRate = baseRate ;
   for (int j=0; j<=numRateIncrs; j++)
   {
     cout << setw(9) << 100.0*titleRate <<"%" << setw(3)<< "";
     titleRate += rateIncrement;
   }
  cout << endl << endl;

  cout << setprecision(2) ;
  payment = basePayment ;   
  for (int i=0; i<=numPmtIncrs; i++)
  {
    cout << setw(10) <<  payment;
    rate = baseRate ;
    for (int j=0; j<=numRateIncrs; j++)
    {
      temp1 =  rate/12 ;
      temp2 =  temp1*1000.00/payment ;
      months = -log( 1.0 - temp2)/log(1.0+temp1) ;
      interest = months*payment - 1000.00 ;
      cout  << setw(10) << interest << "|" << setw(2) ;
      if (months < 1000000) cout << int(0.5 + months/12);
      else cout << "!" ;
      rate += rateIncrement ;
    }
    cout << endl ;
    payment += pmtIncrement ;
  }

   cout << endl << endl ;
   cout << "(Keep in mind that after the the loan is paid off," << endl ;
   cout << "one can invest the amount of the payment instead" << endl ;
   cout << "each month.  This can make the difference in wealth" << endl ;
   cout << "due to paying off the loan early much greater than " << endl ;
   cout << "what this chart shows.) " << endl << endl ;

  return 0 ;
}