Most of the software engineers I know here in Bangalore have either already bought a house or are planning to buy one. The biggest incentives are perhaps the easy availability of home loans at interest rates far lower than that available to the previous generation and the tax-breaks one gets here in India on the principal and the interest paid on home loans. Our generation is also not averse to taking on a debt unlike the previous generation. In addition, many of us feel that it is better to take the plunge now and enjoy the comforts of your own house than to diligently save all the required money for years and then buy a house, only to find out that the dream was realised a bit too late in your life.

When one takes a loan, a natural question that comes to mind is how much would the EMI (Equated Monthly Installment) be that one has to pay back to the bank every month. Finding out the EMI for different tenures of the home loan allows one to select the best tenure based on one's current and projected income and expenses and possibly other factors.

What I find is that most software engineers would rather construct or borrow elaborate spreadsheets for this purpose or use a web-based EMI calculator rather than calculating it themselves. Some brave souls ask around for the formula to calculate EMIs and then try to write a programme that calculates the EMIs for them. I find it particularly appalling that very few bother to figure it out for themselves, especially since it involves elementary algebra that most of us have surely learnt in high school. It either reflects the creeping sloth and sloppiness in our generation or the rote learning and regurgitating of formulae that our system of education seems to encourage. In any case, I attempt to show in this post how simple it really is to figure out how to calculate EMIs on your own and to hopefully encourage my fellow engineers into applying the basic mathematics they learn in school to real-life problems themselves instead of looking around to see if someone else has solved it for them.

For the sake of simplicity, assume that the loan is offered on a "monthly rest" basis. That is, the bank calculates the interest at the end of every month on the amount you still owe to the bank at the beginning of the month, adds it to the amount you already owe and then deducts your EMI from this to calculate the total amount you still owe to the bank at the beginning of the next month. Some banks offer loans on a "daily rest" basis, where the outstanding amount and the interest is recalculated every day, but you still pay back on a monthly basis. The older "annual rest" basis is no longer in use as far as I can tell. Note that it is easy to adapt the formula given here to the "daily rest" basis and that is, of course, left as an exercise for the reader.

Suppose you take on a loan for P Rupees, the tenure of the loan is n months (for example, n=240 for a 20-year loan), the monthly rate of interest is r (usually calculated by dividing the annual rate of interest quoted by the bank by 12, the number of months in a year, and dividing that by 100 as the rate is usually quoted as a percentage) and E Rupees is the EMI you have to pay every month. Let us use P_{i} to denote the amount you still owe to the bank at the end of the i-th month. At the very beginning of the tenure, i=0 and P_{0}=P, the principal amount you took on as a loan.

At the end of the first month, you owe the bank the original amount P, the interest accrued at the end of the month r×P and you pay back E. In other words:

_{1}= P + r×P - E

or to rewrite it slightly differently:

_{1}= P×(1 + r) - E

Similarly, at the end of the second month the amount you still owe to the bank is:

_{2}= P

_{1}×(1 + r) - E

or substituting the value of P

_{1}we calculated earlier:

_{2}= (P×(1 + r) - E)×(1 + r) - E

and once again expanding it and rewriting it slightly differently:

_{2}= P×(1 + r)

^{2}- E×((1 + r) + 1)

where "x

^{y}" denotes "x raised to the power y" or "x multiplied by itself y times". To make this look slightly simpler, we substitute "(1 + r)" by "t" and now it looks like this:

_{2}= P×t

^{2}- E×(1 + t)

Continuing in this fashion and calculating P

_{3}, P

_{4}, etc. we quickly see that P

_{i}is given by:

_{i}= P×t

^{i}- E×(1 + t + t

^{2}+ ... + t

^{i-1})

At the end of n months (that is, at the end of the tenure of the loan), the total amount you owe to the bank should have become zero. In other words, P

_{n}=0. This implies that:

_{n}= P×t

^{n}- E×(1 + t + t

^{2}+ ... + t

^{n-1}) = 0

which means that:

^{n}= E×(1 + t + t

^{2}+ ... + t

^{n-1})

We can simplify this further by noticing that we have a geometric series of n terms here with a common ratio of t and a scale factor of 1. The sum of such a series is given by "(t

^{n}- 1)/(t - 1)", which we substitute in the above equation to yield:

^{n}= E×(t

^{n}- 1)/(t - 1)

which can be rewritten as:

^{n}×(t - 1)/(t

^{n}- 1)

which can again be rewritten by substituting the value of t back as "(1 + r)" as:

^{n}/((1 + r)

^{n}- 1)

and this is the formula for calculating your EMI. This formula can also be rendered more clearly as:

Suppose you take a loan from a bank of 10,00,000 Rupees for 15 years at 8.5% annual rate of interest calculated on a monthly rest basis. In that case, P = 10,00,000, n = 15×12 = 180 and r = (8.5/12)/100 = 0.0070833333. Putting these values into the formula given above gives us E = 9847.40 (approximately).

When you write a programme to calculate your EMI using the formula given above, be careful to structure your computations to accommodate loss of precision and rounding errors. For more information, read "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (PDF) by David Goldberg or section 4.2, "Floating Point Arithmetic", in "The Art of Computer Programming, Volume 2: Seminumerical Algorithms" (3rd Edition) by Donald Knuth.

Update (2006-12-19): Clarified how "r" is calculated from the quoted interest rate and added an example to illustrate the application of the formula. Added references to Goldberg's article and Knuth's book for the caveats to bear in mind while using floating-point arithmetic.

Update (2007-02-01): The only intent of this post was to show how basic mathematics can be used to calculate EMIs. I am just an ordinary computer programmer and not a financial consultant. Please stop asking me for advice on your loan plans!