display all even and odd integers between 2 user input numbers

also posted in vb6 forum. not sure to which one this applies.

Page 1 of 1

1 Replies - 19004 Views - Last Post: 23 March 2009 - 01:58 PM Rate Topic: -----

#1 keminkwitz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-March 09

display all even and odd integers between 2 user input numbers

Post icon  Posted 23 March 2009 - 01:46 PM

using for...next loop the program is supposed to accept 2 user inputed integers and display all even numbers between the two in one label and all odd numbers between in another label.


	Private Sub displayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles displayButton.Click
		' displays the odd and even numbers between two integers
		Dim intNum1 As Integer
		Dim intNum2 As Integer
		Dim intOdd As Integer
		Dim intEven As Integer

		Integer.TryParse(number1TextBox.Text, intNum1)
		Integer.TryParse(number2TextBox.Text, intNum2)

		For counter As Integer = intNum1 To intNum2
			If (intNum1) Mod 2 <> 0 Then

				oddLabel.Text = intNum1.ToString & vbNewLine
				intNum1 = intNum1 + 1




			Else

				evenLabel.Text = intNum1.ToString & vbNewLine
				intNum1 = intNum1 + 1





			End If



		Next counter


	End Sub




I'm not exactly sure what this code is doing. When I put 1 in the first number and 10 in the second number, the odd label only shows 9 and the even label only shows 10. Another example is when I put 2 and 15, the odd label comes out as 15 and the even label is 14. I'm very new to VB and programming in general and have just started getting into for...next loops and don't fully understand what the counter is or how the loop is supposed to work. I guess I'm having 2 problems. One is that I don't know how to make the program find the right numbers(my logic is off? or how I set up the loop isn't right?) and the other is the labels are showing only 1 number each instead of all the numbers in between on their own line. Thanks a lot to anyone who can help me out.

Is This A Good Question/Topic? 0
  • +

Replies To: display all even and odd integers between 2 user input numbers

#2 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: display all even and odd integers between 2 user input numbers

Posted 23 March 2009 - 01:58 PM

ok each time you go through and erase the label and replace it


you need something like:
oddLabel.Text = oddLabel.Text & intNum1.ToString & vbNewLine



but there is a way to make this code easier to understand:
For i as integer = intNum1 To intNum2
			If i Mod 2 <> 0 Then
				oddLabel.Text = oddLabel.Text & i.ToString & vbNewLine
			Else
				evenLabel.Text = evenLabel.Text & i.ToString & vbNewLine
			End if
Next i



you don't need a counter with a for loop, it does that part for you!

This post has been edited by krum110487: 23 March 2009 - 01:58 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1