home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / PWBTUTOR / ANNUITY2.C$ / ANNUITY2
Encoding:
Text File  |  1991-07-02  |  1.9 KB  |  71 lines

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