home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume4 / loan < prev    next >
Encoding:
Text File  |  1989-02-03  |  3.5 KB  |  139 lines

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