C# - Calculate EMI


C# - Calculate EMI

CODE
using System; class Program { static void Main() { // Princile Amount double p = 20000; // Number of Months double n = 24; // Rate of Interest per annum; Example: 7% double r = 10; //Calculate rate per month double rm = r / 12 / 100; // EMI can be calculated using the formula below. // [P x R x (1+R)^N]/[(1+R)^N - 1)] double monthlyPayment = (p * rm * Math.Pow((1 + rm), n)) / (Math.Pow((1 + rm), n) - 1); double yearlyPayment = monthlyPayment * n; Console.WriteLine("Monthly Payment: {0:0.00}", monthlyPayment); Console.WriteLine("Yearly Payment: {0:0.00}", yearlyPayment); Console.WriteLine("{0, -10} {1, 10} {2, 20} {3, 20}", "Month", "Interest", "Principle", "Balance"); double currentBalance = p; for(int i = 1; i <= n; i++) { double interest = currentBalance * rm; double principle = monthlyPayment - interest; currentBalance = currentBalance - principle; Console.WriteLine("{0, -10} {1, 10:0.00} {2, 20:0.00} {3, 20:0.00}", i, interest, principle, currentBalance); } //OUTPUT //Monthly Payment: 922.90 //Yearly Payment: 22149.56 //1 166.67 756.23 19243.77 //2 160.36 762.53 18481.23 //3 154.01 768.89 17712.35 //4 147.60 775.30 16937.05 //5 141.14 781.76 16155.29 //6 134.63 788.27 15367.02 //7 128.06 794.84 14572.18 //8 121.43 801.46 13770.72 //9 114.76 808.14 12962.58 //10 108.02 814.88 12147.70 //11 101.23 821.67 11326.03 //12 94.38 828.51 10497.52 //13 87.48 835.42 9662.10 //14 80.52 842.38 8819.72 //15 73.50 849.40 7970.32 //16 66.42 856.48 7113.84 //17 59.28 863.62 6250.22 //18 52.09 870.81 5379.41 //19 44.83 878.07 4501.34 //20 37.51 885.39 3615.95 //21 30.13 892.77 2723.18 //22 22.69 900.21 1822.98 //23 15.19 907.71 915.27 //24 7.63 915.27 -0.00 } }

Comments