Loops and functions in Visual Basic?
ok, i have a project that im working on and i cant seem to get it to function. the program runs, but instead of displaying the rate like i need it only shows the words. Here is my code. and the instruction for the program. Ive spent 2 weeks working on this and still cant figure it out.
Write a program to solve the following problem: A TV set is purchased with a loan of $563 to be paid off with 5 monthly payments of $116. The interest rate is 1 percent per month. Display a table giving the balance on the loan at the end of each month.
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer = 1
month = 1
Do Until month = 5 'Repeat loop until month is 5
If month > 5 Then Exit Do ' if month is greater than 5 end the loop cycle
month += 1
DisplayTtl(563)
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "Month", "Rate"))
ListBox1.Items.Add(String.Format(fmtStr, "month", "rate"))
'KNOW this is the problem line, just not sure what it should be instead to make it display the ascending months and descending rate
Loop
'No end if statement needed because no if statement used above
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
'Show rate as the balance with interest less monthly amount
rate = (balance * 0.01) - 116
Return rate
End Function
End Class
Answer 1 all i get is a 5 with $110.37 in. the list box
It's better than having just a 0 like I did, but i need the months listed with the amounts so it should end up looking like:
1 (rate)
2 (rate)
3 (rate)
4 (rate)
5 (rate)
ok we have months 2-5 showing. missing month 1. Also the rate is appearing the same in each month instead of decreasing as the months progress :S
ok i fixed the months. :) made it a 0 at top not a 1
ok i fixed the months. :) made it a 0 at top not a 1
all 5 months show but the balance is still the same in each one as opposed to descending from 563 in month one down.
What if we used an array for each of the month results? That way one could reference the next.
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer = 1
Dim StartValue As Double = 563
month = 1
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "Month", "Rate"))
ListBox1.Items.Add(String.Format(fmtStr, month, DisplayTtl(StartValue)))
Do Until month = 5
If month > 5 Then Exit Do
month += 1
ListBox1.Items.Add(String.Format(fmtStr, month, DisplayTtl(StartValue)))
Loop
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
Dim StartValue As Double
rate = (balance * 0.01) - 116
StartValue = rate
Return rate
End Function
End Class
This is what I have now. It still isn't returning the correct rates just $110.37 over and over.
month 1 balance should be 563, then month 2 447 etc until 0 at month 5.
monthly balance = total (563 or lower progression + 0.01) - 116
sorry meant to be a * as in *0.01
So close with that last one, only instead of $563 its putting out $5.63, rate is descending though. I had the same idea using If statements.
everything works as needed except why it is coming as $5.63 instead of $563 as the first balance. after that its perfect :)
making it a 1 seems to make the balance go up instead of down. Weird because I would think that would have made sense.
yes
Public Class Form1
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
Dim month As Integer
Dim balance As Double = 563
Dim rate As Double
month = 1
Dim fmtStr As String = "{0,12} {1, 4:c2}"
ListBox1.Items.Clear()
ListBox1.Items.Add(String.Format(fmtStr, "Month", "Rate"))
ListBox1.Items.Add(String.Format(fmtStr, month, DisplayTtl(balance)))
Do Until month = 5
If month > 5 Then Exit Do
month += 1
balance = balance + rate - 116
rate = DisplayTtl(balance)
ListBox1.Items.Add(String.Format(fmtStr, month, rate))
Loop
End Sub
Private Function DisplayTtl(ByVal balance As Double) As Double
Dim rate As Double
rate = (balance * 1.01) - 116
Return rate
End Function
End Class
everything is great guys! thank you. I have a successful running program! YAY!!!!