c / c++javascriptmysqltools / utilities

The Simple Mortgage Calculator Implemented in C/C++, Javascript and MySQL

Although there are many available mortgage calculators out there, the principle of a simple mortgage calculator is really simple, which is based on the following math formula.

c = \frac{rP(1+r)^N}{(1+r)^N-1}

where, the c is the monthly repayment, r is the monthly interests (which is the one-twelveth of the yearly interests). P is the total money borrowed. N is the total number of re-payments (e.g. the number of months).

Simple Mortgage Calculator Implemented in MySQL

For example, I borrowed 170K from the HSBC Bank, the interests rate (yearly) is 2.49%, and I need to repay the principle+interests in 25 years. The amount of repayment each month (suppose it is fixed-rate) can be computed via the following command in MySQL console:

mysql> select 0.0249/12*170000*power(1+0.0249/12,25*12)/(power(1+0.0249/12,25*12)-1);
+------------------------------------------------------------------------+
| 0.0249/12*170000*power(1+0.0249/12,25*12)/(power(1+0.0249/12,25*12)-1) |
+------------------------------------------------------------------------+
|                                                       761.792586559163 |
+------------------------------------------------------------------------+
1 row in set (0.00 sec)

Simple Mortgage Calculator Implemented in C/C++

With the pow function from math.h, the payment function is defined as follows:

1
2
3
4
5
6
7
8
9
#include <math.h>
 
// r - monthly interest rate
// p - principle
// n - number of repayments
double payment(r, p, n) {
    var x = pow(1 + r, n);
    return r * p * x / (x - 1);
}

Online Simple Mortgage Calculator Implemented in Javascript

Similarly, the function defined in Javascript is defined as:

1
2
3
4
5
6
7
// r - monthly interest rate
// p - principle
// n - number of repayments
function payment(r, p, n) {
    var x = Math.pow(1 + r, n);
    return r * p * x / (x - 1);
}

Fill in the values of r, P and N and the the following online mortgage calculator (open in new window) will compute the monthly repayment and interests (not available if you are viewing in Accelerated Mobile Page version):

Principle (money borrowed):

Interests Rate%:
Repayment Years:
Years
Monthly Repayment: 761.79.
Total Payments: 228537.78.
Total Interest: 58537.78 (25.61%).

mortgage calculator with formula

The alternative version of this mortgage calculator can be found here.