home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / PWBTUTOR / ANNUITY1.C$ / ANNUITY1
Encoding:
Text File  |  1991-11-26  |  2.0 KB  |  75 lines

  1. //   ANNUITY1.C - PWB tutorial example program
  2. #pragma message (\
  3. "--\n"\
  4. "--  ANNUITY1.C - Generate annuity table.\n--\n" \
  5. "--  NOTE: Contains intentional errors for use with the PWB Tutorial\n--")
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. void main( void )
  12. {
  13.     float Principal, Rate, Pmt, RatePct, PerInterest, PerPrin;
  14.     int Nper;
  15.     int ActNper;
  16.     ont Period;
  17.  
  18.     //
  19.     // Get input from the user.
  20.     //
  21.  
  22.     printf( "\nEnter Present Value: " );
  23.     scanf ( "%f", &Principal );
  24.     printf( "\nEnter Interest Rate in Percent: " );
  25.     scanf ( "%f", &Rate );
  26.     printf( "\nEnter Number of Periods in Years:  );
  27.     scanf ( "%i", &Nper );
  28.  
  29.     //
  30.     //  Calculate periodic percentage as a fraction (RatePct),
  31.     //  number of periods in months (ActNper). Then, calculate
  32.     //  the monthly payment (Pmt).
  33.     //
  34.  
  35.     RatePct = Rate / 1200.0;
  36.     ActNper = Nper * 12;
  37.     Pmt = Principal * (RatePct / (1.0 - (1.0 /
  38.           pow( 1.0 + RatePct, ActNper ))));
  39.  
  40.     //
  41.     //  Print a summary of the annuity
  42.     //
  43.     printf( "\n\n"
  44.     "Principal:       %13.2f\n"
  45.     "Interest Rate:   %13.2f\n"
  46.     "Number of Years: %13i\n"
  47.     "Monthly Payment: %13.2f\n"
  48.     "Total Payments:  %13.2f\n"
  49.     "Total Interest:  %13.2f\n\n\n",
  50.     Principal, Rate, Nper, Pmt,
  51.     Pmt * (float)Nper * 12.0,
  52.     Pmt * (float)Nper * 12.0 - Principal );
  53.  
  54.     //
  55.     //  Print headings of the amortization table.
  56.     //
  57.     printf( "Period Year   Principal Interest\n"
  58.             "------ ------ --------- --------\n" );
  59.  
  60.     //
  61.     //  Loop on the number of periods, printing the period, year,
  62.     //  interest portion and principal portion of each payment.
  63.     //
  64.  
  65.     for( Period = 1; Period <= ActNper; Period++ )
  66.     {
  67.     PerInterest = Principal * RatePct;
  68.     PerPrin = Pmt - PerInterest;
  69.     printf( "%6d %6d %9.2f %9.2f\n",
  70.         Period, Period / 12, PerPrin, PerInterest );
  71.     Principal = Principal - PerPrin;
  72.     }
  73.  
  74. }
  75.