“Yes sir, I will take that 10,000 dollar loan. If I pay 100 dollars a month that will be $1200 dollars right there after my first year.” Not so fast the bank teller tells you. You still owe us $9383 and some change, not $8800. You have to remember interest and at your monthly compoundings, it adds up! Damn that interest really hits home. Here I thought I had 1200 paid down and really it was more like almost 700. This is a common situation and sometimes the reason people get in trouble with their loans and credit cards. Luckily for you I will cover an equation for you to help find out what your balance will be given any number of payments over any number of years. And to add a little fun, we will throw it down in PHP. So grab onto that wallet tight, we are about to take a loan out at the Programming Underground!

Banks charge you interest on a loan for of three things. 1) They are taking a risk by giving you a bunch of money. What if you can’t pay it back? They are stuck holding the bag. So they mitigate this risk by collecting interest on everyone so when one person doesn’t pay they are hurting so bad. 2) They are using other people’s money to finance your loan, so there has to be an incentive for those investors to keep their money at the bank right? Thus the bank encourages people to keep money in their system by paying them interest on their savings. 3) They profit from the interest. Ever notice that they charge you more interest on your loan that they pay you on any savings you have? That difference goes right into the bank’s pocket.

So you know what the true secret to wealth is? Put your money in the bank to gain interest, then never take out a loan. If you do need to take out a loan, pay it off as fast as you can! ðŸ˜‰

Now lets assume you took out a loan despite my words of wisdom. You take out a 10,000 dollar loan to help you finance that new porn video store you want to open up. Porn isn’t cheap… well some of it is cheap, but you need the capital to get started. The bank is willing to loan it to you but at an annual percentage rate (APR) of 6%. That is each year it takes the balance and adds on 6% of it back onto what you owe. So if you didn’t pay anything, after the first year you would owe 10600. This calculation would be called “simple interest” since it is simply taking the balance and adding on the interest.

This would be all fine and dandy, but you are paying throughout the year. Your payments should count for something right? Why should you be dinged 600 dollars a year when you don’t have a balance of 10000 anymore. To complicate matters they don’t simply ding your ending balance for the year and take out the interest at once. They divide the yearly interest rate into “compoundings”. A compounding is simply taking the current balance at a given time and applying a portion of the annual percentage rate. So if you have 6% over the year, if we have monthly compoundings it applies the interest calculation every month but at a rate of 6% / 12 or .5%. So as you can imagine the more compoundings the more interest you will owe.

To figure all this out, and find the current balance we owe at a given time, we have to first determine our balance and the interest it has accrued up to that point. Then subtract the payments you have made and how long you have been making payments over the number of years.

To help us do this we can create a rather sophisticated PHP function. We could easily adapt this function to C++ or Java or VB/C#. Why not do it in PHP? It would be fun! Lets take a look…

<?php // Calculate how much is left on a loan given a percentage rate, // number of payments per year (same as compoundings) and after certain number of $years function repay($loan, $payment, $rate = .08, $paymentsPerYear = 1, $years = 30) { $amount_plus_interest = $loan * pow((1.0 + ($rate/$paymentsPerYear)),($paymentsPerYear * $years)); $pay = pow((1.0 + ($rate/$paymentsPerYear)), ($paymentsPerYear * $years)) - 1.0; $pay /= ((1.0 + ($rate/$paymentsPerYear)) - 1.0); return $amount_plus_interest - ($payment * $pay); } // Give us the balance on a $10,000 dollar loan (at 6% APR) if we make $100.00 monthly payments // The loan will compound monthly at the same time payments are due and after 1 year has passed. echo "B = " . repay(10000.00, 100.00, .06, 12, 1); ?>

Now of course this looks really complicated but it is not if you follow it line by line and taking your time. The function takes the loan amount, the payment amount we are making, the annual percentage rate, the number of times we will be paying per year (this also determines how many compoundings will be done each year so if we pay 12 times a year, aka monthly, then the interest will compounding monthly as well), and lastly the number of years.

I have setup some default parameters to create an interest rate of 8%, 1 payment a year, over 30 years. Of course when we call the function we are going to give it 10,000 dollar loan and paying 100 dollar monthly payments at 6% APR. We then want to know the balance remaining after 1 year.

The result of this call gives us about $9383.22 which is essentially the following steps…

1) 10000 dollars at monthly compounding which equals $10616.78 at the end of the year. Notice if we had done simple interest this would have been $10600. By doing 12 compoundings we are paying slightly more. 16 dollars in fact after the first year, but imagine if this was a 300,000 dollar loan for a house! That would be a significant difference!

2) We then calculate the payment which is essential the formula P ((1 + r/n)NT – 1) / ((1 + r/n) – 1).

3) Take the amount, plus the interest, and subtract the payments of N compoundings over T years and we get our current balance.

I hope this makes a bit of sense and proves useful to you the next time you want to take out a loan. Or better yet, give a loan to someone else! Tell your friend you will loan him the money at daily compounding of 8% and watch him cry as you make a profit. That is the price you pay when you take a loan and the reward you get for taking the risk and loaning someone some cash.

Enjoy! ðŸ™‚

© 2021 The Coders Lexicon. All rights reserved. Privacy Policy