mortgage program (source)

joeb at marque.mu.edu joeb at marque.mu.edu
Fri Jan 4 05:40:28 AEST 1991


Here's a simple little mortgage calcuator program.  It prints tables of monthly
payments for different principals and different interest rates; tables are
printed for 15-year and 30-year mortgages.  You can optionally include a
specific principal amount in the table by including it on the command line.
If desired, it should be easy to include a specific interest rate also.
Have fun!

------------------------------------------------------------------------

/*
**  Compute monthly mortgage payments for 15 and 30 year mortgages, at
**  various interest rates.  Expects tab stops every 8 characters.
**
**  Usage:  mortgage [amount]
*/

#define  P_LOW		 50000		/* range of principal */
#define  P_HIGH		200000
#define  P_INCR		 25000

#define  Y_LOW		15		/* range of mortgage terms */
#define  Y_HIGH		30
#define  Y_INCR		15

#define  R_LOW		.085		/* range of interest rates */
#define  R_HIGH		.125
#define  R_INCR		.005

double pow(double,double);

main(argc,argv)
    int argc;
    char *argv[];
{
	int principal, years, amount = 0;
	float rate;
	char did_it;

	if (argc > 1) {
	    if (argv[1][0] == '?') {
		usage();
		exit(1);
	    }
	    amount = atoi(argv[1]);
	}

	for (years=Y_LOW; years<=Y_HIGH; years+=Y_INCR) {
	    printf("\n\t\tMonthly Payment, %d-year mortgage\n",years);
	    printf("Principle\t\tInterest Rate\n");
	    for (rate=R_LOW; rate<R_HIGH+R_INCR/2.; rate+=R_INCR)
		printf("\t  %4.1f%%",rate*100.);
	    printf("\n");
	    for (rate=R_LOW; rate<R_HIGH+R_INCR/2.; rate+=R_INCR)
		printf("\t  -----");
	    printf("\n");

	    did_it = 0;
	    for (principal=P_LOW; principal<=P_HIGH+P_INCR; principal+=P_INCR) {
		if ( amount && (!did_it) &&
		  ((amount < principal) || (principal > P_HIGH)) ) {
		    one_line(amount,years);
		    did_it++;
		}
		if (principal <= P_HIGH)
		    one_line(principal,years);
	    }
	}
}

usage()
{
	printf("Mortgage payment calculator\n\n");
	printf("Usage: mortgage [amount]\n");
}

one_line(amount,years)
    int amount;
    int years;
{
	float rate;

	printf("$%6d",amount);
	for (rate=R_LOW; rate<R_HIGH+R_INCR/2.; rate+=R_INCR)
	    printf("\t%7.2f", (float)
	      (amount * rate/12 / (1 - 1 /
	      (double)pow((double)(1.+rate),(double)years) )) );
	printf("\n");
}

----------------------------------------------



More information about the Alt.sources mailing list