# How to make EMI calculator using C#

This article demonstrate about how to calculate EMI of the loan amount. This example use three textbox controls(loan amount, interest rate and periods In year), one button control with named ‘btnCalculateEMI‘ and one label control named ‘lblMonthlyPayment‘ on which we show monthly payment. Add these control on the windows form like as:

[wpfilebase tag=file id=2]

Now first we understand formula of EMI calculation:
We use this mathmatical formula for calculating EMI

## [Formula]

EMI = [(p*r/12) (1+r/12)^n]/[(1+r/12)^n – 1 ]
where p = principal amount(primary loan amount)
r = rate of interest per year
n = Total number of Years

## [Code]

```private void button1_Click(object sender, EventArgs e)
{
double LoanAmount = 0;
double Payment = 0;
double InterestRate = 0;
int PaymentPeriods = 0;
try
{
InterestRate = Convert.ToDouble(txtRate.Text);
PaymentPeriods = Convert.ToInt16(Convert.ToDouble(txtTime.Text) * 12);
LoanAmount = Convert.ToDouble(txtLoan.Text);
if (InterestRate > 1)
{
InterestRate = InterestRate / 100;
}
Payment = (LoanAmount * Math.Pow((InterestRate / 12) + 1,
(PaymentPeriods)) * InterestRate / 12) / (Math.Pow
(InterestRate / 12 + 1,(PaymentPeriods)) - 1);
lblMonthlyPayment.Text = "Monthly Payment: " + Payment.ToString("N2");
}
catch
{
}
}```

In the above you are seeing the ToString() method with the “N2” argument. The numeric (“N”) format specifier converts a number to a string. The precision specifier indicates the desired number of digits after the decimal point. In the above “2” is the precision. For the example id Payment value is 34 then Payment.ToString(“N2”) will be show the “34.00”.

## Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.

## 10 thoughts on “How to make EMI calculator using C#”

1. Thanks for the code. It looks great.

2. Rahul Verma says:

Thanks. Nice article. Keep it up.

3. Deep says:

if (InterestRate > 1) what is &gt in above code please tell me thanks in advance.

4. Sunil Bansal says:

Sorry to say but i think it shows a wrong output. There is some problem with the formula.
For e.g. :
P: 2000
ROI: 1%
Time: 300 Months

Output: 166.67 (Wrong Output)

Actual Result: 7.54 (Check HDFC & SBI EMI Calculator Online)

5. Ankur Gupta says:

Just update if (InterestRate > 1) with if (InterestRate > 0) and you will got the same result as HDFC & SBI EMI Calculator Online.

Ankur

6. Ankur Gupta says:

And also Time should be in no of YEAR.

7. Bhavesh Patel says:

Thanks. For Help Me.