loan analysis program
stevens at hsi.UUCP
stevens at hsi.UUCP
Thu Feb 2 22:33:26 AEST 1984
The program follows the discussion.
########################################################################
The following is an engineer's perspective of how to calculate
monthly payments for a loan. I an NOT an accountant, but welcome any
comments or criticisms on the following. I was unable to find any
discussion of this topic in the books I have on hand, but am sure that
this stuff is discussed in detail somewhere. My CRC math tables gave
some really strange tables for calculating the present value of a loan
(whatever that is), but nothing about calculating monthly payment.
Sylvia Porter's "Money Book" was equally useless, as was an undergrad
accounting text.
Assume that you borrow an amount A for N months at an interest
rate I. (I assume that the interest rate I is what they call the "APR").
The interest rate must be converted to a decimal value between
0 and 1 corresponding to the period of payment (assumed to be once a
month). That involves taking a published loan rate such as 12.5% and
dividing the 12.5 by 1200 (12 * 100). Assume that you want to pay back
the loan in N equal payments each payment being P. (I assume throughout
that you make payments once a month but clearly this could be generalized
to any payment period.)
When you make each payment of P dollars some portion goes to pay
interest and the remainder goes to pay off the principal (the balance).
Typically the interest is calculated as the rate I times the remaining
balance. At each payment you can calculate the amount of interest you
are paying (I * remaining balance), the amount of the payment applied
to the principal (P - amount of interest) and the new balance of the
loan (previous balance - principal payment). You can form the following
table: (I use x**n to denote x raised to the n'th power - in honor of my
first programming language.)
Payment# Interest Principal Balance
-------- -------- --------- -------
1 IA P - AI A + AI - P
2 I(A + AI - P) P - I(A + AI - P) A + 2AI + A(I**2) - (2P - IP)
This table can be carried on to any payment number. Lets take a numerical
example:
A = $1,000.
I = 12.5 / 1200 (12.5 %)
N = 12 (one year)
The monthly payment comes to P = $89.08 (we'll derive this below) and
the loan analysis is:
month principal interest balance
1 78.66 10.42 921.34
2 79.48 9.60 841.86
3 80.31 8.77 761.55
4 81.15 7.93 680.40
5 81.99 7.09 598.41
6 82.85 6.23 515.56
7 83.71 5.37 431.85
8 84.58 4.50 347.27
9 85.46 3.62 261.81
10 86.35 2.73 175.46
11 87.25 1.83 88.21
12 88.16 0.92 0.05
In order to calculate the monthly payment, note that the final balance
comes out to be zero (or at least close enough for a scientist, the
discrepancy above is due to rounding to assure that every monthly payment
breakdown between interest and principal is accurate to 2 decimal places;
I'm sure accountants have some way to deal with this ??)
Take the case of N=1 (simple interest). The final balance must
equal zero, so
A + AI - P = 0
Solve for P
P = A + AI
or
P = A(1 + I)
which is the equation for simple interest. Now consider the case of N=2.
The final balance must equal zero, so
A + 2AI + A(I**2) - (2P - IP) = 0
Solving for the monthly payment P gives
A(1 + 2I + I**2)
P = ----------------
2 + I
Multiply top and bottom by I
AI(1 + 2I + I**2)
P = -----------------
2I + I**2
then add 1 and subtract 1 from the denominator
AI(1 + 2I + I**2)
P = -----------------
1 + 2I + I**2 - 1
>From high school algebra we have that
(1 + I)**2 = 1 + 2I + I**2
so we have
AI(1 + I)**2
P = --------------
(1 + I)**2 - 1
Indeed, if you feel like going through a lengthy algebra exercise,
you will find that for any value of N
AI(1 + I)**N
P = --------------
(1 + I)**N - 1
which is the formula for calculating the monthly payment. When you
actually calculate the above, you can divide both top and bottom by the
(1 + I)**N term, to avoid calculating it twice (since an exponentiation
may be expensive). Also, this is not a true proof - I did enough
cases to convince myself and the equation does evaluate to values of P
gotten from a real estate book for some test cases. I'm sure the math
purists out there will want to do a proof by induction to prove that
it really holds for any N.
Please send any comments, etc. to:
Richard Stevens
{ decvax | hao | seismo | sdcsvax } ! kpno ! hsi ! stevens
ihnp4 ! hsi ! stevens
##########################################################################
/*
* Calculate loan amortization.
*
* To compile under UNIX:
* cc -O -o loan loan.c -lm
*/
#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 )
char *pname;
main(argc, argv)
int argc;
char *argv[];
{
double atof(), amount, balance, intrate, interest, payment;
double principal, totinterest;
int i, nmonths;
char *s;
pname = argv[0];
amount = 0.0;
intrate = 0.0;
nmonths = 0;
while ((--argc > 0) && ((*++argv)[0] == '-'))
for (s = argv[0]+1; *s != '\0'; s++)
switch (*s) {
case 'a':
if (--argc <=0)
error("-a option requires another argument",
NULL);
amount = atof(*++argv);
break;
case 'i':
if (--argc <=0)
error("-i option requires another argument",
NULL);
intrate = atof(*++argv);
break;
case 'n':
if (--argc <=0)
error("-n option requires another argument",
NULL);
nmonths = atoi(*++argv);
break;
default:
fprintf(stderr, "%s: illegal option %c\n",
pname, *s);
break;
}
/*
* Operator prompts go to stderr, in case you want to
* pipe stdout to a file or a line printer filter.
*/
while (amount <= 0.0) {
fprintf(stderr, "enter amount: ");
scanf("%lf", &amount);
}
while ((intrate <= 0.0) || (intrate > 99.9)) {
fprintf(stderr, "enter interest rate: ");
scanf("%lf", &intrate);
}
while (nmonths <= 0) {
fprintf(stderr, "enter #months: ");
scanf("%d", &nmonths);
}
/*
* Now calculate what is required from the input above.
*/
intrate /= 1200; /* convert to monthly percentage between
0.0 and 1.0 */
payment = ROUND(amount * (intrate / (1.0 - (1.0 /
pow(1.0 + intrate, (double) nmonths)))));
/* monthly payment */
printf("\nmonthly payment = $ %.2f\n",
payment, payment*nmonths);
/*
* Calculate and print amortization schedule.
*/
printf("\nmonth interest principal balance\n\n");
balance = amount;
totinterest = 0.0;
for (i = 1; i <= nmonths; i++) {
interest = ROUND(balance * intrate);
/* amount of this payment that is interest */
totinterest += interest;
principal = payment - interest;
/* amount of this payment going to principal */
balance -= principal;
/* the principal decreases the loan balance */
printf("%3d %10.2f %10.2f %10.2f\n",
i, interest, principal, balance);
}
printf("\nTotal interest = $ %.2f\n", totinterest);
exit(0);
}
error(str1, str2)
char *str1, *str2;
{
fprintf(stderr, "%s: ", pname);
fprintf(stderr, str1, str2);
fprintf(stderr, "\n");
exit(1);
}
More information about the Comp.sources.unix
mailing list