Bank Intrest Calculation for Loans and Savings in C#.net

.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Intrest_Calculator
{
public partial class LoanCalc : Form
{
public LoanCalc()
{
InitializeComponent();
}


private void Calc_Loan_EMI(double Principal_Amount, double Term_Months, double Intrest_Rate)
{
Intrest_Rate = Intrest_Rate / 1200;
double Years = Term_Months / 12;
double Payable_Amount = Principal_Amount * Intrest_Rate / (1 - (Math.Pow(1 / (1 + Intrest_Rate), Term_Months)));
double Total_Amount = Payable_Amount * Term_Months;
double Total_Interest = Total_Amount - Principal_Amount;
double Yearly_Interest = Total_Interest / Years;
double Interest_PA = Yearly_Interest / Principal_Amount * 100;
double Interest_PM = (Yearly_Interest / Principal_Amount * 100) / 12;


labelmonthlyemi.Text = Payable_Amount.ToString("N3");
labeltotamountwithinterset.Text = Total_Amount.ToString("N3");
labelinterestpa.Text = Interest_PA.ToString("N3");
labelinterestpm.Text = Interest_PM.ToString("N3");
labeltotinterest.Text = Total_Interest.ToString("N3");
labelyearlyintersest.Text = Yearly_Interest.ToString("N3");
}


private void Calc_Amortization(double loanAmt, double Term_Months, double interestRate, double Installment_Number, double monthValue, double yearValue)
{
double interestRateForMonth = interestRate / 12; // (Monthly Rate of Interest in %)
double interestRateForMonthFraction = interestRateForMonth / 100; // (Monthly Interest Rate expressed as a fraction)
double emi = calculateEMI(loanAmt, interestRate, Term_Months);


var loanOustanding = loanAmt;
double totalPayment = 0;
double totalInterestPortion = 0;
double totalPrincipal = 0;
string installmentDate = string.Empty;
double interestPortion = 0, principal = 0;


List<CLS_AMORTIZATION> listamort = new List<CLS_AMORTIZATION>();
double month = 0, year = 0;


if (Installment_Number > Term_Months || Installment_Number == 0)
{
//The Installment must be less than or equal to the Tenure
}
else
{
for (int i = 1; i <= Term_Months; i++)
{
CLS_AMORTIZATION obj = new CLS_AMORTIZATION();


if (monthValue != 0)
{
month = monthValue + i - 1;
}
else
{
month = month + 1;
}


if (month > 12)
{
year = yearValue + 1;
yearValue = year;
monthValue = 0;
month = monthValue + 1;
}
else
{
year = yearValue;
}


if (month < 10)
{
installmentDate = "0" + month + "/" + year;
}
else
{
installmentDate = month + "/" + year;
}


if (loanOustanding == loanAmt)
{
loanOustanding = loanAmt;


obj.INSTALLMENTNO = i.ToString();
obj.INSTALLMENTDATE = installmentDate;
obj.OPENINGBALANCE = loanOustanding.ToString();
obj.EMI = emi.ToString();


totalPayment = totalPayment + emi;
interestPortion = loanOustanding * interestRateForMonthFraction;
interestPortion = roundDecimals(interestPortion, 0);
}
else
{
obj.INSTALLMENTNO = i.ToString();
obj.INSTALLMENTDATE = installmentDate;
obj.OPENINGBALANCE = loanOustanding.ToString();
obj.EMI = emi.ToString();


totalPayment = totalPayment + emi;
interestPortion = loanOustanding * interestRateForMonthFraction;
interestPortion = roundDecimals(interestPortion, 0);
}


loanOustanding = loanOustanding + interestPortion - emi;
loanOustanding = roundDecimals(loanOustanding, 0);


obj.LOANOUTSTANDING = loanOustanding.ToString();
obj.INTEREST = interestPortion.ToString();


totalInterestPortion = totalInterestPortion + interestPortion;
principal = roundDecimals(emi - interestPortion, 0);


obj.PRINCIPAL = principal.ToString();


totalPrincipal = totalPrincipal + principal;


listamort.Add(obj);
}


dataGridView1.DataSource = listamort;
}
}


private double calculateEMI(double loanAmt, double interestRate, double tenure)
{
if (interestRate != 0)
{
double interestRateForMonth = interestRate / 12; // (Monthly Rate of Interest in %)
double interestRateForMonthFraction = interestRateForMonth / 100; // (Monthly Interest Rate expressed as a fraction)
double emi = 1 / Math.Pow((1 + interestRateForMonthFraction), tenure);
double emiPerLakh = (loanAmt * interestRateForMonthFraction) / (1 - emi); // (EMI per lakh borrowed)
emiPerLakh = roundDecimals(emiPerLakh, 0);
return emiPerLakh;
}
else
{
double emi = loanAmt / tenure;
double emiPerLakh = roundDecimals(emi, 0);
return emiPerLakh;
}
}


private double roundDecimals(double original_number, int decimals)
{
double result1 = original_number * Math.Pow(10, decimals);
double result2 = Math.Round(result1);
double result3 = result2 / Math.Pow(10, decimals);


return (result3);
}


public class CLS_AMORTIZATION
{
public string INSTALLMENTNO { get; set; }
public string INSTALLMENTDATE { get; set; }
public string OPENINGBALANCE { get; set; }
public string EMI { get; set; }
public string LOANOUTSTANDING { get; set; }
public string INTEREST { get; set; }
public string PRINCIPAL { get; set; }
}


private void eMIToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBoxcalcemi.Visible = true;
}


private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}


private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{


}


private void buttonCalculateemi_Click(object sender, EventArgs e)
{
try
{
double Loan_Amt = Convert.ToDouble(textBoxloanamount.Text), Tenture = Convert.ToDouble(textBoxtenture.Text), Interest = Convert.ToDouble(textBoxInterestRate.Text);
Calc_Loan_EMI(Loan_Amt, Tenture, Interest);
Calc_Amortization(Loan_Amt, Tenture, Interest, 1, DateTime.Now.Month, DateTime.Now.Year);
groupBoxLoandetails.Visible = true;
groupBoxrepaydetails.Visible = true;
}
catch (Exception ex)
{
groupBoxLoandetails.Visible = false;
groupBoxrepaydetails.Visible = false;
MessageBox.Show("Sorry! there is a error: " + ex.Message);
}
}
}
}

You Can Download the Working Code From here.