From: lcrowley@sandia.uucp (Linda Crowley)
Newsgroups: misc.consumers.house
Subject: reply to request for amortization program
Date: 10 Apr 92 14:10:41 GMT
Distribution: usa
Organization: Sandia Natl Labs, Div. 9224


Since this got bounced back to me twice after I sent it back to the
requestor via email, I guess I'll send it out this way.  (I original
request was purged.)  This was in response to the mail from 
rac@peewee.unx.dec.com asking for a C program to compute monthly
mortgage payments.
  
I wrote a simple interactive C program to compute mortgage payments.
The principal, interest rate, frequency and number of payments are
all variable (but it doesn't compute "variable-rate" interest).
It's crude and I wrote it originally intending for only my eyes to see it :) 


Here's an example of using it, specifying an 8.75% annual interest rate
on an $120,000 loan, for 15 years (12 payments per year):

=============================================================
amort
 principal? 120000
 annual percentage rate? 8.75
 number of pmt periods? (number or 'x*y' notation) 15*12
 number of payments per year? 12
 Principal:        120000.00
 APR:              8.75
 number of pmt periods=180 (12 per year for 15.00 years)


 Payment per pEriod:          1199.34
 Total interest paid:         95880.91
 Principal paid on first pmt: 324.34
 Interest paid on first pmt:  875.00

  Do you want a schedule? (n) n
=============================================================

If you had answered "y" to the last question, it would have given
you the option to put the schedule in a file, or to stdout.

To compile the source on a UNIX machine, do the following:
  cc -o amort amort.c -lm
(the -lm is to load the math library)

I don't know how to compile/load on a non-UNIX machine, or if the
file pointer assignment to stdout in the code would work on a 
non-UNIX-based machinE - I would suppose it would..


Here's the source (not well-documented!) :

=============================================================
#include <stdio.h>
#include <math.h>
#include <strings.h>

/* Linda Crowley, 1/92 */
/* compile/load with:  cc -o amort amort.c -lm  on UNIX-based system */

main()
{

	char inline[100];

	float princ;	/* beginning principal */
	float apr;	/* annual percentage rate (0-100) */
	int num_pmts;	/* number of total payment periods */	
	int ppy;	/* number of payments per year */
	float intpp;	/* fractional interest per payment period */
	float pmt;	/* resulting payment per period */
	float num_years;
	float int_pmt;
	float p_pmt;
	int i,j,yr;
	float ix,iy;
	double x,y,dpmt;
	FILE *fp;

	/*		           princ * intpp
		pmt =	  ----------------------
				  1 - (1+intpp)**(-num_pmts)   	*/

	printf(" principal? ");
	scanf("%f",&princ);
	printf(" annual percentage rate? ");
	scanf("%f",&apr);

	printf(" number of pmt periods? (number or 'x*y' notation) ");
	fgets(inline,100,stdin);
	fgets(inline,100,stdin);
	if (strchr(inline,'*') != NULL) {
		sscanf(inline,"%f*%f",&ix, &iy);
		num_pmts = (int)(ix*iy);
	}
	else {
		sscanf(inline,"%d",&num_pmts);
	}
		
	printf(" number of payments per year? ");
	scanf("%d",&ppy);
	num_years = (float)(num_pmts)/(float)(ppy);

	printf(" Principal:        %.2f\n",princ);
	printf(" APR:              %.2f\n",apr);
	printf(" number of pmt periods=%d (%d per year for %.2f years)\n",
		num_pmts,ppy,num_years);

	intpp = apr/(float)(100 * ppy);
	x = (double)(1. + intpp);
	y = (double)(-num_pmts);
	dpmt = (double)(princ * intpp)/((double)(1.0) - pow(x,y));
	pmt = (float)dpmt;
	int_pmt = intpp * princ;
	p_pmt = pmt - int_pmt;

	printf("\n\n Payment per period:          %.2f\n",pmt);
	printf(" Total interest paid:         %.2f\n",
		(float)(num_pmts)*pmt - princ);
	printf(" Principal paid on first pmt: %.2f\n",p_pmt);
	printf(" Interest paid on first pmt:  %.2f\n",int_pmt);

	printf("\n  Do you want a schedule? (n) ");
	scanf("%s",inline);
	if (strchr(inline,'y') != NULL || strchr(inline,'Y') != NULL) {
		printf(" Output where? (filename or 0 for stdout) ");
		scanf("%s",inline);
		if (inline[0] == '0')
			fp = stdout;
		else {
			if ((fp=fopen(inline,"w")) == NULL) {
				printf(" could not open %s\n",inline);
				exit(1);
			}
			fprintf(fp," Principal:        %.2f\n",princ);
			fprintf(fp," APR:              %.2f\n",apr);
			fprintf(fp," number of pmt periods=%d (%d per year for %.2f years)\n",
				num_pmts,ppy,num_years);
			fprintf(fp," Payment per period:          %.2f\n",pmt);
			fprintf(fp," Total interest paid:         %.2f\n\n",
				(float)(num_pmts)*pmt - princ);
		}

		fprintf(fp,
		"Pmt#(year)       Princ.Pmt		Int.Pmt             Princ.Remaining\n");
		yr = 1;
		for (j=1,i=1; i<=num_pmts; j++,i++) {
			if (j>12) {
				j=1;
				yr++;
			}
			int_pmt = intpp * princ;
			p_pmt = pmt - int_pmt;
			princ -= p_pmt;
			fprintf(fp,"  %d(%d)            %.2f            %.2f              %.2f\n",
				i,yr,p_pmt,int_pmt,princ);
		}
	}

}