Question VB code for Diminishing Loan Balance

J3Brother

New member
Joined
Mar 3, 2011
Messages
3
Programming Experience
Beginner
Hey Guys,

I'm new to VB and taking an online course and need help.

Here is the problem. I am to write a VB program that will take a Loan of $563 that is to be paid off with 5 monthly payments of $116. The interest Rate is 1% per month. I need to display a table giving the number of months and the balance on the loan after each month.
The Table displayed should look like this:

Month Amount Owed
1 $452.63
2 $341.16
3 $228.57
4 $114.86
5 $0.00

Here is my code but I do not understand why it only shows "Month" and "Amount Owed" and nothing else. Any help would be greatly appreciated. Maybe someone can point me in the right direction and I can figure it out.

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

Dim numMonths As Integer = 1
Dim Payment As Double = 116.0
Dim Balance As Double = 563.0
Dim Rate As Double = 0.01
Dim EndBal As Double
lstResult.Items.Clear()
Dim fmtStr As String = "{0, 2}{3, 10:C}"

Do While Balance = 0
numMonths += 1
EndBal += Balance * Rate - Payment

Loop

lstResult.Items.Add(
"Months" & "Ammount Owed")

End Sub
 
Here's your own code:
VB.NET:
Dim Payment AsDouble = 116.0
[U]Dim [B]Balance[/B] AsDouble [B]= 563.0[/B][/U]
Dim Rate AsDouble = 0.01
Dim EndBal AsDouble
lstResult.Items.Clear()
Dim fmtStr AsString = "{0, 2}{3, 10:C}"

[U]DoWhile [B]Balance = 0[/B][/U]
Now do you see the problem?
 
Thanks for your reply jm. I tried "Do While Balance = 563" This locked up the program. and also "Do While < 563" still the values do not show up.
 
still the values do not show up.


Well when you update your listbox, you are not actually putting any variables in there?

lstResult.Items.Add("Months" & "Ammount Owed")
Output: MonthsAmountOwed

Dim a as integer =10
lstResult.Items.Add("Amount of A is " & a.tostring)
Output: Amount of A is 10

Also, don't you need to move the code to update your list, in to your loop? Otherwise it will just update the final result.


Why don't you concentrate on just making the loop work with simple data first, so the listbox updates, and then start adapting it to work with your criteria? So make a listbox display 1 to 10 for example.
 
Hey Guys,

Thanks for all your responses. After tinkering around a bit, I've decided a whole new aproach. Instead of using the Do Loop, I've decided to use the For Next Loop. This seems to be a lot easier and shorter!
Check it out:

Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

Dim Balance As Double

Balance = 563

lstResult.Items.Clear()
Dim fmtStr As String = "{0, 6}{1, 10:C}"

lstResult.Items.Add(
String.Format("Months" & "Ammount Owed"))

For numMonths As Integer = 1 To 5
Balance = Balance - 116 + (Balance * 0.01)
lstResult.Items.Add(
String.Format(fmtStr, numMonths, Balance))
Next
End Sub

This worked!
Thanks again everyone!
 
Back
Top