Amortization… it is the process of accounting for, and usually decreasing, an amount of money over a period of time. You can amortize a mortgage or various types of loans, and you can amortize assets based on their value and what their value will be in so many periods of time. Many newbies on our boards have asked about this very project as part of the computer science courses and while there are many ways to calculate it, even using the amortization formula P = (i * Principle x (1 + i)^n) / ((1 + i)^n – 1), not all of them make it simple and straight forward. I have set out to show a simple process of creating an amortization schedule using a loan amount and annual percentage rate to calculate an unknown term for a loan… for example purposes I start with a payment which is 1/20th of the loan amount. I will take this process through the five languages as part of my “definitive” series of blog entries…. right here on the Programming Underground!

Amortization schedules have a few characteristics which show you that you are doing them right. To understand these characteristics, we will start with an example where Joe Schmoe takes out a loan for a whopping $100 dollars! Joe is the man now, drippin with ice and all the ladies want to be next to Joe’s side. That is just how he rolls. But anyway, the bank tells Joe that to be loaned the 100 dollars, he will have to make payments of $5 dollars a month plus an annual interest rate of 10%. Joe agrees… and why not, he gets the ladies because of it!

Every month the loan balance gains interest which is then added to the loan. When Joe pays his 5 dollars, his whole 5 dollars is not going to go towards the 100 dollar loan. Some of it will have to go to pay off the interest that built up in the last month. Perhaps his 100 dollar loan accrues .83 cents of interest during the first month. So when Joe pays his 5 dollars, he is actually paying the .83 cents and the rest goes towards the 100 dollars. 5 – .83 = $4.17. So the balance after paying the interest is 100 dollars – 4.17 which equals 95.83. You would expect to have only 95.00 but because of the interest eating up a portion of his payment, his payment doesn’t go as far as it use to.

What does this all mean? Well, it means a few things. First it means that it is going to take Joe longer to pay back that loan since he has to pay this extra cost of an interest rate. Being that 5 dollars is 1/20th of his loan, you can believe that it will take him more than 20 months to pay it all back. It also means that after he does pay it all back, he would have paid more than 100 dollars. That is, 100 dollars plus all the interest it accrued over the last several months. It also means the more we can get towards that principle amount, the less interest it can accrue and the faster Joe can pay off that loan!

So as we look at an amortization schedule we will see a few things… 1) The payment amount will be the same over the life of the loan. 2) The balance will decrease, but slower than you might expect. 3) The interest amount accrued each period will lower too since it is based on the lowering balance. 4) The amount towards the principle will rise since less interest is eating away at your payment every month. 5) After everything is said and done, you should have a zero balance and your last payment will most likely be less than what you had been paying during the course of your loan. So on Joe’s last payment he will have to pay only $4.85 and not the 5 dollars he had been paying before.

Below is an example screen of what our amortization schedule for Joe Schmoe looks like…

How do we get these results? Well, lets take a magical journey through time and space. We will wander through the trees and mountains and along the path of multiple computer languages. Our first stop, C++!

#include <iostream> #include <iomanip> using namespace std; int main () { // Make sure we use types that hold decimal places double new_balance, ending_balance; double interest_paid, annual_rate, principle_paid, payment; cout << "Please enter the original loan amount $ (-1 to end the program):"; cin >> ending_balance; cout << "Enter the interest rate on your loan %:"; cin >> annual_rate; cout << endl; // Setup the stream to show dollar amount formatting cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); // Setup a counter to count payments int count = 1; // Get our standard payment which is 1/20 of loan payment = (ending_balance / 20.0); while (ending_balance > 0.0) { new_balance = ending_balance; // Calculate interest by multiplying rate against balance interest_paid = new_balance * (annual_rate / 12.0); // Subtract interest from your payment principle_paid = payment - interest_paid; // Subtract final payment from running balance ending_balance = new_balance - principle_paid; // If the balance remaining plus its interest is less than payment amount // Then print out 0 balance, the interest paid and that balance minus the interest will tell us // how much principle you paid to get to zero. if ((new_balance + interest_paid) < payment) { cout << count << ". Payment: $" << (new_balance + interest_paid) << " Interest: $" << interest_paid << " Principle: $" << (new_balance - interest_paid) << " Loan Balance is: $0.00" << endl; } else { // Lets show the table, loan, interest, and payment made towards principle cout << count << ". Payment: $" << payment << " Interest: $" << interest_paid << " Principle: $" << principle_paid << " Loan Balance is: $" << ending_balance << endl; } count++; } system("pause"); return 0; }

As you can see we capture the loan amount and annual percentage rate (as a decimal). We then calculate the payment simply as being 1/20th of the loan amount. Here you could create the payment amount if you know the length of the term. He we are just going to make a guess at 20 months roughly. We then go into a loop in which we apply the payment to the balance for each month. We calculate the interest amount (before we apply a payment) by dividing our annual percentage rate (note the word annual so we have to divide it by 12 to get months) and store that into a variable called interest_paid. We then subtract that amount out of the payment Joe made. Leaving us with an amount which we can apply towards the principle on the loan. We subtract that amount from the loan and we get an ending balance. We keep doing this until we reach a balance of zero. I have added a counter to each payment so when printed you can see how many months it took to pay off the loan. As you can see in the screenshot, Joe had to pay his 5.00 dollars for about 22 months. Which you can quickly figure out that he paid roughly 2 months of interest (little less than 10 dollars). Joe thus paid about 110.00 dollars to repay his 100 dollar loan. Who gets the extra 10? The banks and the people who put their money into a savings account so that it can be loaned to Joe.

How about we take a spin with this on Java?

import java.util.Scanner; import java.text.DecimalFormat; public class amortization { public static void main(String args[]) { // Make sure we use types that hold decimal places double new_balance = 0.0, ending_balance = 0.0; double interest_paid = 0.0, annual_rate = 0.0, principle_paid = 0.0, payment = 0.0; Scanner input = new Scanner(System.in); System.out.print("Please enter the original loan amount $ (-1 to end the program): "); ending_balance = input.nextDouble(); System.out.print("Enter the interest rate on your loan %: "); annual_rate = input.nextDouble(); System.out.println(); DecimalFormat f = new DecimalFormat("$#,###,###.00"); // Setup a counter to count payments int count = 1; // Get our standard payment which is 1/20 of loan payment = (ending_balance / 20.0); while (ending_balance > 0.0) { new_balance = ending_balance; // Calculate interest by multiplying rate against balance interest_paid = new_balance * (annual_rate / 12.0); // Subtract interest from your payment principle_paid = payment - interest_paid; // Subtract final payment from running balance ending_balance = new_balance - principle_paid; // If the balance remaining plus its interest is less than payment amount // Then print out 0 balance, the interest paid and that balance minus the interest will tell us // how much principle you paid to get to zero. if ((new_balance + interest_paid) < payment) { System.out.println(count + ". Payment: " + f.format(new_balance + interest_paid) + " Interest: " + f.format(interest_paid) + " Principle: " + f.format(new_balance - interest_paid) + " Loan Balance is: $0.00"); } else { // Lets show the table, loan, interest, and payment made towards principle System.out.println(count + ". Payment: " + f.format(payment) + " Interest: " + f.format(interest_paid) + " Principle: " + f.format(principle_paid) + " Loan Balance is: " + f.format(ending_balance)); } count++; } } }

Again this code is very similar to C++ and the only real differences is using a scanner class to capture user input and printing out values using the help of a DecimalFormat object called “f”. We also use the System.out.println() function instead of cout.

How about C#? Let’s do that!

private void btnCalculate_Click(object sender, EventArgs e) { // Make sure we use types that hold decimal places double new_balance, ending_balance; double interest_paid, annual_rate, principle_paid, payment; ending_balance = Convert.ToDouble(txtLoanAmount.Text); annual_rate = Convert.ToDouble(txtAPR.Text); // Setup a counter to count payments int count = 1; // Get our standard payment which is 1/20 of loan payment = (ending_balance / 20.0); while (ending_balance > 0.0) { new_balance = ending_balance; // Calculate interest by multiplying rate against balance interest_paid = new_balance * (annual_rate / 12.0); // Subtract interest from your payment principle_paid = payment - interest_paid; // Subtract final payment from running balance ending_balance = new_balance - principle_paid; // If the balance remaining plus its interest is less than payment amount // Then print out 0 balance, the interest paid and that balance minus the interest will tell us // how much principle you paid to get to zero. if ((new_balance + interest_paid) < payment) { listDisplay.Items.Add(count + ". Payment: " + (new_balance + interest_paid).ToString("C") + " Interest: " + interest_paid.ToString("C") + " Principle: " + (new_balance - interest_paid).ToString("C") + " Loan Balance is: $0.00"); } else { // Lets show the table, loan, interest, and payment made towards principle listDisplay.Items.Add(count + ". Payment: " + payment.ToString("C") + " Interest: " + interest_paid.ToString("C") + " Principle: " + principle_paid.ToString("C") + " Loan Balance is: " + ending_balance.ToString("C")); } count++; } }

Here I put the code into a button called btnCalculate and to get the info from the user, I read in data from two textboxes. One for the loan amount (which is converted to a double datatype) and one for APR (again converted to double). I then display each line in a listbox called listDisplay. You could easily add this to a listview or a multi-line textbox. It is up to you. Notice the use of ToString with a parameter of “C”. this will convert the double into a string which represents a “currency” value. Just another formatting technique.

As you can see, this will be very similar in VB.NET. The only thing that really changes is the syntax for defining variables and creating if statements / while loops.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' Make sure we use types that hold decimal places Dim new_balance, ending_balance As Double Dim interest_paid, annual_rate, principle_paid, payment As Double ending_balance = Convert.ToDouble(txtLoanAmount.Text) annual_rate = Convert.ToDouble(txtAPR.Text) ' Setup a counter to count payments Dim count As Integer = 1 ' Get our standard payment which is 1/20 of loan payment = (ending_balance / 20.0) While (ending_balance > 0.0) new_balance = ending_balance ' Calculate interest by multiplying rate against balance interest_paid = new_balance * (annual_rate / 12.0) ' Subtract interest from your payment principle_paid = payment - interest_paid ' Subtract final payment from running balance ending_balance = new_balance - principle_paid ' If the balance remaining plus its interest is less than payment amount ' Then print out 0 balance, the interest paid and that balance minus the interest will tell us ' how much principle you paid to get to zero. If (new_balance + interest_paid) < payment Then listDisplay.Items.Add(count & ". Payment: " + (new_balance + interest_paid).ToString("C") & " Interest: " & interest_paid.ToString("C") & " Principle: " & (new_balance - interest_paid).ToString("C") & " Loan Balance is: $0.00") Else ' Lets show the table, loan, interest, and payment made towards principle listDisplay.Items.Add(count & ". Payment: " + payment.ToString("C") & " Interest: " & interest_paid.ToString("C") & " Principle: " & principle_paid.ToString("C") & " Loan Balance is: " & ending_balance.ToString("C")) End If count += 1 End While End Sub

Nothing too big there. Again, it is just formatted into VB syntax and displayed, like the C# example, into a listbox called listDisplay. We also use the ToString(“C”) method here as well and everything else is pretty much the same structure.

How about you web gurus out there? Want some PHP to wet your palette? Here you go…

<?php function amortization($loanAmount = 0.0, $apr = 0.0) { // Setup our ending balance and annual rate $ending_balance = $loanAmount; $annual_rate = $apr; // Setup a counter to count payments $count = 1; // Get our standard payment which is 1/20 of loan $payment = ($ending_balance / 20.0); while ($ending_balance > 0.0) { $new_balance = $ending_balance; // Calculate interest by multiplying rate against balance $interest_paid = $new_balance * ($annual_rate / 12.0); // Subtract interest from your payment $principle_paid = $payment - $interest_paid; // Subtract final payment from running balance $ending_balance = $new_balance - $principle_paid; // If the balance remaining plus its interest is less than payment amount // Then print out 0 balance, the interest paid and that balance minus the interest will tell us // how much principle you paid to get to zero. if (($new_balance + $interest_paid) < $payment) { echo "$count. Payment: $". number_format($new_balance + $interest_paid,2) ." Interest: $". number_format($interest_paid,2) ." Principle: $". number_format($new_balance - $interest_paid,2). " Loan Balance is: $0.00<br/>\n"; } else { // Lets show the table, loan, interest, and payment made towards principle echo "$count. Payment: $". number_format($payment,2) ." Interest: $" .number_format($interest_paid,2) ." Principle: $". number_format($principle_paid,2) ." Loan Balance is: $". number_format($ending_balance,2). "<br/>\n"; } $count++; } } amortization(100.00,.10); ?>

The things to note in here is the use of the number_format() function to give us our dollar value formatting and a bit of rounding to two decimal places. We also structure this similar to the C++ example but with the classic PHP function signature style. At the end we call the function as we would any other PHP function. The results will be formatted for one result per line. Throw it into a table if you like or use some CSS to make it all pretty.

So this concludes our definitive look at an amortization / mortgage schedule! Not too difficult once you understand the principles behind it. Now I want to remind you all that this is for figuring out an unknown term length and if you are provided a term length (such as 24 months) you will want to calculate your payment based on that term length. You may also want to take a closer look at the formula for amortization mentioned earlier.

Understanding the principle behind amortization schedules will not only help you solve any programming problems with amortization, but will also help you determine if your results are correct. Compare your program results with amortization calculators on the net and always double check your work for errors.

Enjoy and thanks for reading! ðŸ™‚

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