[alt.sources] mortgage calculator

sparks@corpane.UUCP (John Sparks) (08/16/89)

Here is a newer version of the mortgage calculator program that was posted here
a while back. I added a way to keep track of additional principal paid, and I
reformatted the output to give more useful information, including keeping track
of total interest, and principal on a month to month and yearly basis.

It's still not perfect. I haven't added much error trapping or anything. If you
enter nothing when asked for a number, it might crash. I never bothered to fix
that. But I thought maybe some of you out there might want it, anyway.

Here it is:
-----------------8<----------------------------------------------

echo x - mort.c
sed 's/^X//' >mort.c <<'*-*-END-of-mort.c-*-*'
X/*
X *
X * loan.c
X * @(#) loan.c amoritization program
X * downloaded from usenet and modified by John Sparks
X * added: principal payments, reformatted output.
X *
X * compile with: cc -O mort.c -o mort -lm
X */
X
X#include <stdio.h>
X#include <math.h>
X
X/*
X *
X */
X 
Xmain()         /* loan program */
X{
X     float amt, term, rate, ic;
X     float r, temp, pmt, prpmt, fv;
X     float exp, prin, x, y, mbeg, mnbr, yrint = 0;
X     float yrprin = 0, acprin = 0, acint = 0, actotal = 0;
X     int month, i, k, a = 0, yr=1;
X     char d, filename[9], c;
X     FILE *fp;
X     /*  prompt for input from terminal  */
X     printf(" This Program is provided for your personal information
only.\n");
X     printf(" DISK claims no responsibility as to the accuracy"); 
X     printf(" of the data provided.\n\n");
X     
X     printf("Enter principal amount: ");
X     scanf("%f", &amt);
X
X     printf("Enter term in years: ");
X     scanf("%f", &term);
X
X     printf("Enter interest rate in percent (ex: 10 = 10 percent): ");
X     scanf("%f", &rate);
X
X     
X     printf("Enter additional Principal payment (Enter 0 if none): ");
X     scanf("%f", &prpmt);
X     printf("Enter month payments begin (ex: 8 = August): ");
X     scanf("%f", &mbeg);
X
X     /*  compute payment and future value  */
X
X     r = rate/100.0;
X     x = 1.0 + r/12.0;
X     y = term*12.0;
X     temp = (1.0 / pow(x,y));
X     pmt = (amt*r/12.0) / (1-temp);
X     k = term*12;
X     fv = pmt*k;
X     ic = fv - amt;
X
X     printf("Do you want the report directed to a file or to the screen?");
X     printf("\n[f = file / s = screen] : ");
X     d = getchar();      /* This is only here to eat up the '\n' left over
X                      from the last scanf. */
X     d = getchar();
X     switch(d) {
X          case 'f':
X          d = getchar();
X          printf("\nEnter a filename: ");
X          scanf("%s", filename);
X          while((c = getchar()) != '\n') {
X          filename[a] = c; a++; }
X          fp = fopen(filename, "w");
X          break;
X          default:
X          fp = stdout;
X          break;
X          }
X
X          /*  print header  */
X  
X        fprintf(fp,"\n\t\t *** Amortization Schedule ***\n\n");
X     if (prpmt > 0) {
X   fprintf(fp,"With no Additional Principal Payment your loan would be:\n");
X          }
X        fprintf(fp,"Ammount:  %.2f\t", amt);
X        fprintf(fp,"Rate:  %.3f\t",rate);
X        fprintf(fp,"Term:  %.1f YRS\t", term);
X        fprintf(fp,"Payment:  %.2f\n", pmt);
X        fprintf(fp,"Total cost of Loan:  %.2f\t",fv);
X        fprintf(fp,"Total Interest Paid: %.2f\n", ic);
X     if (prpmt > 0) {
X    fprintf(fp,"With an additional monthly Principal Payment of: %.2f",prpmt);
X        }
X           fprintf(fp,"\nYour payment schedule will be:\n");
X           fprintf(fp,"\n\n          ----------- Monthly -----------");
X           fprintf(fp,"  --------- Accumulated ---------");
X        fprintf(fp,"\nMONTH     PRINCIPAL   INTEREST    BALANCE");
X        fprintf(fp,"  PRINCIPAL   INTEREST      TOTAL\n");
X  
X        /* start of loop to print amortization schedule */
X  
X        mnbr=mbeg;
X        for (i=1; amt > 0; i++) {
X               month = i;
X               exp = amt*(r/12.0);
X               yrint=yrint+exp;
X               prin = (pmt-exp)+prpmt;
X          if (amt < pmt) {
X               prin=amt;
X               pmt=prin+exp;
X               }
X               amt = amt-prin;
X          yrprin=yrprin+prin;
X          acprin=acprin+prin;;
X          acint=acint+exp;
X          actotal=actotal+pmt;
X
X           mnbr++;
X           fprintf(fp,"%3d    \t %10.2f %10.2f %10.2f %10.2f %10.2f %10.2f\n",
X          month, prin, exp, amt, acprin, acint, actotal);
X 
X           if (mnbr > 12 ) {
X           fprintf(fp,"\n       \t %10.2f %10.2f = Total for Year %d\n\n",
X          yrprin,yrint,yr);
X           yr++;
X           yrint=0;
X          yrprin=0;
X
X           mnbr=1;
X              }
X}
Xif (mbeg > 1) {
X     fprintf(fp,"\n       \t %10.2f %10.2f = Total for Year %d\n\n",
X          yrprin, yrint, yr);
X     }
X     fclose(fp);
X
X}
X/*-------end of file----------*/
*-*-END-of-mort.c-*-*
exit


-- 
John Sparks   |  {rutgers|uunet}!ukma!corpane!sparks | D.I.S.K. 24hrs 1200bps
|||||||||||||||          sparks@corpane.UUCP         | 502/968-5401 thru -5406 
Chicken Little only has to be right once.