[net.sources] yalp - Yet Another Loan Program

covert@ihuxq.UUCP (covert) (04/20/84)

	<A line for the demons on the net>

	There has been some interest lately concerning loan programs.
	So, therefore here are some observations from a course that I took
in Engineering Economics. The following equations are from the textbook
'Principles of Engineering Economy' written by Eugen L. Grant, W. Grant 
Ireson, and Richard S. Leavenworth. Published by John Wiley&Sons.

	The equation used to determine monthly payments is the to determine
your annuity based upon a fixed deposit with a fixed interest rate over
a fixed period of time. To wit:

	A = P * [ i(i+1)^n / ((i+1)^n -1)	]	*Equ. 4, page 33

	Where:
	P = the Present sum of money (or Principal)
	i = interest rate per interest period
	n = the number of interest periods (length of loan/deposit)

	This equation can be used to tell you how much you would need to 
deposit today to earn an annuity based upon today's interest rate, and
the length of time that the money will be drawing interest.

	Another useful equation is one to determine how much money you
could borrow for a fixed monthly payment. This would be solved by:

	P = A * [ ((i+1)^n-1) / (i*(i+1)^n)) ]	*Equ. 6, page 33

	This equation would also tell you the Present Value of a series of
deposits spread over a period of time and earning a fixed interest rate.

	BTW, I have found that this textbook is an excellent reference book
for all forms of investment decisions. Truly an excellent book.

	Now, for my contribution to the art of programming, a simple little
gem to show how your monthly payment varies based upon the current interest
rate.
	I leave it to the reader to expand upon this program.

STRIP UPTO & INCLUDING THE DASHED LINE -rec
-----------------------------------------------------------------------
/* EMACS_MODES: tabstop=4, c, !save, !fill, !lnumb */
/*
 * Calculate loan amortization table based at a range of interest
 * rates.
 *
 * To compile under UNIX:
 *	cc -O -o loan loan.c -lm
 *
 * Author: R. E. Covert
 * ATT-T,BTL ihnp4!ihuxq!covert
 */

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

/*
 * Convert a double value to a dollars value with exactly two digits
 * to the right of the decimal point.  Round the third digit to the
 * right of the decimal point.
 */

#define	ROUND(x) ( (double) ( (long) ((x + 0.005) * 100.0) ) / 100.0 ) 

main()
{
	double	atof();
	double	lo_int_rate = 0.0;	/* lowest interest rate */
	double	hi_int_rate = 0.0;	/* highest interest rate */
	double	amount = 0.0;		/* amount of loan */
	double	irate = 0.0;		/* current interest rate */
	double	apr_rate = 0.0;		/* APR interest rate */
	double	delta;
	double	payment = 0.0;		/* monthly payment */
	int	i;
	int		nmonths = 0;		/* length of loan */

	/*
	 * Operator prompts go to stderr, in case you want to
	 * pipe stdout to a file or a line printer filter.
	 */

	while (lo_int_rate <= 0.0 || lo_int_rate > 99.9) {
		fprintf(stderr, "Enter the lowest desired interest rate (APR) :");
		scanf("%lf", &lo_int_rate);
		fprintf(stderr, "\n");
	}
	while (hi_int_rate <= 0.0 || hi_int_rate > 99.9) {
		fprintf(stderr, "Enter highest desired interest rate (APR) :");
		scanf("%lf", &hi_int_rate);
		fprintf(stderr, "\n");
	}
	if (hi_int_rate <= lo_int_rate ) {
		fprintf(stderr, "Highest interest rate MUST be higher then the\n");
		fprintf(stderr, "lowest interest rate.\n");
		fprintf(stderr, "\n");
		exit(1);
	}
	
	while (amount <= 0.0) {
		fprintf(stderr, "Enter loan amount: ");
		scanf("%lf", &amount);
		fprintf(stderr, "\n");
	}

	while (nmonths <= 0) {
		fprintf(stderr, "Enter length of loan in months: ");
		scanf("%d", &nmonths);
		fprintf(stderr, "\n");
	}

	/*
	 * Now calculate what is required from the input above.
	 */

	lo_int_rate /= 1200;	/* convert to monthly percentage between */
							/* 0.0 and 1.0 */
	hi_int_rate /= 1200;	/* convert to monthly percentage between */
							/* 0.0 and 1.0 */
	printf("\n");	
	printf("Interest       Monthly        Amount Of      Length Of\n");
	printf("Rate (APR)     Payment          Loan           Loan\n");
	printf("-------------------------------------------------------");
	printf("\n");
	
	/* calculate table based upon lo & hi rates */
	delta = 0.1 / (100.0 * 12.0);	/* change by one tenth of one percent */
	for (irate = lo_int_rate; irate <= hi_int_rate; irate += delta) {

		/* calculate monthly payment */
		payment = ROUND(amount * (irate / (1.0 - (1.0 / 
		    pow(1.0 + irate, (double) nmonths)))));

		apr_rate = irate * 1200.0;
		/* print out current values */
		printf("%-4.2f %c        ", apr_rate, '%');
		printf("$%-14.2f", payment);
		printf("$%-14.2f", amount);
		printf("%-4d months\n", nmonths);
		
	}
	exit(0);
}
-- 
			Richard Covert
			AT&T Bell Laboratories
			...ihnp4!ihuxq!covert
			(312) 979-7488