home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume04 / loan < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  3.7 KB

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