# Mortgage Calculator in C++

Hello Learners, today we are going to learn a very interesting topic that is, mortgage calculator using C++. The main function of the mortgage calculator is used to calculate the monthly payment of the amount that is to be paid to the respective bank.

## C++ Program for mortgage calculator

The program is very easy to understand if you know the mortgage calculator formula. Mortgage calculator formula is given below:

M= P*r*(1+r)^n/(1+r)^n-1;

M= monthly payment

P= Principal amount

r= Annual percentage rate

n= Number of repayments(i.e, number of months)

If we know mortgage formula then it is very easy to calculate the mortgage value(i.e,  monthly payment). Remember when we are using the mortgage formula the APR is annual percentage rate, for that first we need to convert it into the percentage format and dividing it by 12 because it is an annual.

The number of years should be converted into the months, just multiply the given years into 12 months then you will get the total number of months. In this code the monthly payment is calculated with the help of a mortgage calculator formula, let’s see the C++ code:

```#include<iostream>
using namespace std;
int main()
{
float m,t,p,s,deno,num,f,x;
int y,i,n;
i=1;
f=1;
cout<<"enter the principal amount:\n";
cin>>p;
cout<<"enter the annual percentage interest rate:\n";
cin>>t;
cout<<"how many years:\n";
cin>>y;
x=t/1200;
n=(y*12);
s=1+(x);
while(i<=n)
{
f=f*s;
i++;
}
num=f*p*(x);
deno=f-1;
m=num/deno;
cout<<"Monthly payment is Rs "<<m<<endl;
return 0;
}

```

Output:

```enter the principal amount:
180000
enter the annual percentage interest rate:
3.75
how many years:
30
Monthly payment is Rs 833.66```

If you have any doubts, you can ask in the comment section given below.

Also, read: Unnamed and inline namespaces in C++