Visual Basic loan interest and principal calculator

Posted 25 November 2012 - 09:33 PM

I'm trying to get the program to output the principal paid out to and the interest paid towards a loan of $5000. No input is take its all been declared already.Monthly payment is correct at 430.33. However, principal and interest output is (interest) 23.6588644... and -453.991013030... (which are flipped in placement). The output should be a row of principal reflecting the amount principal paid out through out the 12 months and a second row displaying also the interest paid towards the loan of 5000 through the 12 months. When i run the code with just the area below count+=1 and above the if statement only one output is displayed. I'm confused why with the loop the number get mixed up.

Re: Visual Basic loan interest and principal calculator

Posted 26 November 2012 - 10:07 AM

First, move the Financial.Pmt to before the Do loop. Since you are using constants in the calculation, rather than variables, there is no need to recalculate the same information every time through the loop.

Now place a breakpoint on that same line, run the code, and click the button. When it stops, single-step the program (Debug menu->step into) to execute the highlighted statement. Now drive your cursor over the result (monthlyPayments), and check the value. Notice anything strange? It's a negative value, which in terms of a loan, means that you add more money to the principal. I think (though I have never used the Financial class), that this function serves for annuities as well as loans, and a negative monthly payment is appropriate for an annuity. Look up Math.Abs to see how to fix this.

Now look at the statement toInterest = monthlyPayments - interest and ask yourself if this is really what you want to do. What value should toInterest contain after this statement?