home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / unix_c / utils / loan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-21  |  3.3 KB  |  131 lines

  1. From riacs!eos!ames!mailrus!tut.cis.ohio-state.edu!cwjcc!hal!ncoast!allbery Mon Sep 26 07:47:27 PDT 1988
  2.  
  3. Posting-number: Volume 4, Issue 90
  4. Submitted-by: "Jane Medefesser" <jane@tolerant.UUCP>
  5. Archive-name: loan
  6.  
  7. I have been using this program for years (at least 4!! :-) )
  8. When we bought our house, it calculated the payment EXACTLY
  9. as the bank did. Currently set up to compile on BSD and SysV - 
  10. pretty straight forward stuff...
  11.  
  12. ====
  13. hit return to continue
  14.  
  15.  
  16. ============================================================================
  17. /*
  18.  *
  19.  * loan.c
  20.  * @(#) loan.c amoritization program
  21.  * 
  22.  * acronym:    loan amortization program
  23.  *             ----
  24.  *
  25.  * purpose:    calculates monthly payment, future value
  26.  *        and creates an amortization schedule
  27.  *
  28.  * 06/27/84    Bill Gregg, Informatics General Corp.
  29.  * 07/12/84     Revision 1
  30.  * 07/12/84    Revision 2
  31.  * 11/05/85    Jane Medefesser, Implementation NPSN Unix (wilbur)
  32.  *        compile as follows: 'cc -o <outfile> loan.c -lm'
  33.  * 12/05/85    Changes to direct output to a file.
  34.  * 03/02/88    Implemented on Eternity 5.3.1
  35.  *
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <math.h>
  40.  
  41. /*
  42.  *
  43.  */
  44.  
  45. main()         /* loan program */
  46. {
  47.     float amt, term, rate, ic;
  48.     float r, temp, pmt, fv;
  49.     float exp, prin, x, y, mbeg, mnbr, yrint = 0;
  50.     int month, i, k, a = 0, yr=1;
  51.     char d, filename[9], c;
  52.     FILE *fp;
  53.     /*  prompt for input from terminal  */
  54.  
  55.     printf("Enter principal amount: ");
  56.     scanf("%f", &amt);
  57.  
  58.     printf("Enter term in years: ");
  59.     scanf("%f", &term);
  60.  
  61.     printf("Enter interest rate in percent: ");
  62.     scanf("%f", &rate);
  63.  
  64.     printf("Enter month payments begin (ex: 8 = August): ");
  65.     scanf("%f", &mbeg);
  66.  
  67.     /*  compute payment and future value  */
  68.  
  69.     r = rate/100.0;
  70.     x = 1.0 + r/12.0;
  71.     y = term*12.0;
  72.     temp = (1.0 / pow(x,y));
  73.     pmt = (amt*r/12.0) / (1-temp);
  74.     k = term*12;
  75.     fv = pmt*k;
  76.     ic = fv - amt;
  77.  
  78.     printf("Do you want the report directed to a file or to the screen?");
  79.     printf("\n[f = file / s = screen] : ");
  80.     d = getchar();      /* This is only here to eat up the '\n' left over
  81.                    from the last scanf. */
  82.     d = getchar();
  83.     switch(d) {
  84.          case 'f':
  85.          d = getchar();
  86.          printf("\nEnter a filename: ");
  87.          scanf("%s", filename);
  88.          while((c = getchar()) != '\n') {
  89.          filename[a] = c; a++; }
  90.          fp = fopen(filename, "w");
  91.          break;
  92.          default:
  93.          fp = stdout;
  94.          break;
  95.          }
  96.  
  97.          /*  print header  */
  98.   
  99.        fprintf(fp,"\n\t *** Amortization Schedule ***\n\n");
  100.        fprintf(fp,"Principal:  %.2f\n", amt);
  101.        fprintf(fp,"Future value:  %.2f\n", fv);
  102.        fprintf(fp,"Term of loan in years:  %.1f\n", term);
  103.        fprintf(fp,"Interest Rate:  %3.3f\n", rate);
  104.        fprintf(fp,"Total Interest Charge:  %.2f\n", ic);
  105.        fprintf(fp,"Payment:  %.2f\n", pmt);
  106.        fprintf(fp,"\nMONTH\tPRINCIPAL\tINTEREST\tBALANCE\n");
  107.   
  108.        /* start of loop to print amortization schedule */
  109.   
  110.        mnbr=mbeg;
  111.        for (i=1; i<=k; i++) {
  112.           month = i;
  113.           exp = amt*(r/12.0);
  114.           yrint=yrint+exp;
  115.           prin = pmt-exp;
  116.           amt = amt-prin;
  117.           mnbr++;
  118.           fprintf(fp,"%d\t %.2f\t\t %.2f\t\t %.2f\n", month, prin, exp, amt);
  119.           if (mnbr > 12 ) {
  120.           fprintf(fp,"\t\tInterest paid for year %d is %.2f\n\n",yr,yrint);
  121.           yr++;
  122.           yrint=0;
  123.           mnbr=1;
  124.               }
  125. }
  126.          fprintf(fp,"\t\tInterest paid for year %d is %.2f\n\n",yr,yrint);
  127.          fclose(fp);
  128. }
  129.  
  130.  
  131.