Resolved Amortization Schedule : MonthlyPrincipal not calculated correctly

learner

Member
Joined
Sep 26, 2020
Messages
8
Programming Experience
Beginner
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

  • console answer.png
    console answer.png
    27.2 KB · Views: 23
  • Online Calculator.png
    Online Calculator.png
    23.1 KB · Views: 23
Last edited by a moderator:
Welcome to the forum. In the future, please post your code in code tags. I've done that for you above.
 
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);

}

I found the mistake that I was making. The EMI should be constant throughout the loan period, but I was calculating it within the the loop, which changed it each time with the new principal.
 
Good job finding the issue. I will mark this thread as resolved. We do not delete threads so that others have an opportunity to learn.
 
Back
Top Bottom