# Visual Basic loan interest and principal calculator

Page 1 of 1

## 2 Replies - 7438 Views - Last Post: 26 November 2012 - 10:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=301613&amp;s=480f20e6b01847cffac59fe23822f693&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Montes_x

Reputation: 0
• Posts: 2
• Joined: 25-November 12

# 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.

```   Private Sub calButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calButton.Click
Dim monthlyPayments As Double
Dim bloanTotal As Double = 5000
Dim rate As Double = 0.005
Dim interest As Double
Dim toPrincipal As Double
Dim toInterest As Double
Dim counter As Integer

Do
'calculate the montly payments.
monthlyPayments =
Financial.Pmt(0.06 / 12, 12, 5000)
TextBox1.Text = monthlyPayments.ToString("C2")

counter = counter + 1        'add one to the counter
interest = bloanTotal * rate    'calculate interest rate from payment that will only count as interest
toInterest = monthlyPayments - interest 'amount of monthly payment going towards interest
toPrincipal = monthlyPayments - toInterest ' amount of monthly payment going towards principal
bloanTotal = bloanTotal - toPrincipal 'new balance after deducing principal from begining balance

Label2.Text = toPrincipal.ToString & "      " &
toInterest.ToString &
ControlChars.NewLine ' concatinate principal and interest strings and create a new line

If counter = 12 Then 'exit loop once counter has reached 12 and loan amount has reached zero
Exit Do
End If
Loop
End Sub

```

Is This A Good Question/Topic? 0

## Replies To: Visual Basic loan interest and principal calculator

### #2 zeeshanef

Reputation: 18
• Posts: 76
• Joined: 14-April 12

## Re: Visual Basic loan interest and principal calculator

Posted 26 November 2012 - 09:27 AM

If 'Label2' is the output of your program then:
Try to use "Label2.Text +=" instead "Label2.Text =".

### #3 lar3ry

• Coding Geezer

Reputation: 312
• Posts: 1,295
• Joined: 12-September 12

## 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?