I am trying to calculate the Amortization Schedule. The monthly Payments are being calculated in a separate method monthlyPayments() . I compared it with a Amortization Schedule calculator online and the problem is while the principle balance(newPrincipleBalance), monthlyInterest , EMI(monthly payments) are being calculated correctly , the monthlyPrincipal is not. It keeps decreasing instead of increasing. What can I do to correct it? I have attached my console answer and online calculator for comparison. This is my code. Thank you!!
}
C#:
decimal principal = 10000;
decimal rate =0;
decimal EMI;
decimal monthlyInterest;
decimal monthlyPrincipal;
decimal newPrincipalBalance;
for (int i = 0; i <= 24; i++)
{
Console.WriteLine("principal " + principal);
EMI = Math.Round(monthlyPayments(principal, 5, 2));
Console.WriteLine("EMI " + EMI);
monthlyInterest = (principal * rate) / 12;
monthlyInterest = Math.Round((principal * 5 / 100) / 12);
Console.WriteLine("monthlyInterest " + monthlyInterest);
monthlyPrincipal = Math.Round(EMI - monthlyInterest);
Console.WriteLine("monthlyPrincipal " + monthlyPrincipal);
newPrincipalBalance = Math.Round(principal - monthlyPrincipal);
Console.WriteLine("newPrincipalBalance " + newPrincipalBalance);
Console.WriteLine("===================================");
principal = newPrincipalBalance;
}
public static decimal monthlyPayments(decimal principal, decimal rate, int years)
{
rate = rate / 1200;
years = years * 12;
decimal F = (decimal)Math.Pow((double)(1 + rate), years);
return principal * (rate * F) / (F - 1);
}
Attachments
Last edited by a moderator: