Calculating Mortgage Payments in C#

For my students’ final project in their current course, I decided to have them program a small C# program that would calculate mortgage figures and compound interest results.  That description doesn’t really do justice to the horrors I visited on these pour souls during their final week before the close of the gradebook but it’s a decent summary for a lesson plan.

If you want to skip the gory details, you can scroll to the bottom of this page for the C# code or download my solution which is available on Github and includes calculations for compound interest over years with or without additional deposits.

The idea came to me when I was leaving my bank one day and thinking about the next class assignment.  It started simply as “Hey … compound interest … yeah!!” and evolved to include the following.

Create a Windows Forms application that will enable the user to calculate a mortgage calculator that will provide the amount of the monthly payment, total interest over the course of the loan, total payment with interest and pay-off date on a loan. The user must be able to specify:
– The original amount
– The down payment
– The loan principal (the amount loaned). (This can be calculated.)
– The length of the loan in months.
– The interest rate

The first part of the assignment was to actually research the calculations needed for the program and to verify them from two different sites.  As the deadline approached, I finally provided the mortgage payment calculation from WikiHow (where one of the students had already found it) and

In this formula, P is the amount of the loan, r is the monthly interest (i.e. for 3% interest: .03 / 12 = .0025) and n is the number of payments or months over which the loan is scheduled. Of course, some reformatting of the above is required in order to put it into C#.

To represent exponents like the one shown on in the top and bottom of the above formula, C# provides the Math.Pow() function which accepts a number and raises it to a specified power.

Math.Pow(1+r), n)

It’s a good idea to use parentheses to at least clarify the intended order of operations so the fractional part of the formula becomes:

(r * (Math.Pow(1 + r, n))) / (Math.Pow(1 + r, n) – 1)

Then enclose the entire formula in parentheses and add the loan amount as the multiplier.

P * ((r * (Math.Pow(1 + r, n))) / (Math.Pow(1 + r, n) – 1))

The variable names might change but that’s the basic representation in C#. For a mortgage of $50,000 at 4% interest over 60 months, the numbers would look like this:

50000 *((.0033 * (Math.Pow(1 + .0033, 60))) / (Math.Pow(1 + .0033, 60) – 1))

For the final C# function:

 public double MonthlyPayment()
   // Calculating the monthly mortgage payment from formula at:
   // P *((r * (Math.Pow(1 + r, n))) / (Math.Pow(1 + r, n)–1))

   double returnPayment = 0;
   double monthlyInterest = _interestRate / 12;
   double loanAmt = LoanPrincipal();

     if (loanAmt > 0 && _interestRate > 0)
       returnPayment = loanAmt * ((monthlyInterest * Math.Pow((1 + monthlyInterest), 
         _loanInMonths)) / (Math.Pow(1 + monthlyInterest, _loanInMonths) - 1));
       returnPayment = 0;
   catch (Exception ex)
     throw ex;

   return returnPayment;

My program allows the user to enter the original amount and a down payment and stores them in class properties. The LoanPrincipal() function returns the difference.

As I often do, I had fun going beyond the assignment with the solution and the entire thing is available on Github.

Once you have the monthly payment, the other statistics are easy to calculate:

Loan Total = Monthly Payment * Number of Months

Total Interest = Loan Total – Loan Principal

Overall, this was a good assignment that gave the students experience in translating math formulas into C# and aiming for the kind of deadlines they’ll see out in the real world.  By the day it was due, they were skipping breaks to hammer out the last of the code. If their brains were a bit fried by the end, at least they got a weekend free of homework to recuperate.

Sign up for our newsletter to receive updates about new projects, including the upcoming book "Self-Guided SQL"!

We respect your privacy and will never share your information with third-parties. See our privacy policy for more information.