18/01/2013

How to display all odd numbers from given range of numbers? VB.Net Code

Loop structures allow you to execute one or more lines of code repetitively. VB.Net support the following loop statements:
  • Do.....loop
  • While.....End While
  • For.......Next
So we can use any one of the loop statement given above to find out the odd number  between the given range of numbers. for example, if you want to display all the odd numbers between two given numbers then you can create a Windows forms like this:
The use any one below mentioned code on the click event of the button.
1 .  Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        Dim num1, num2 As Integer
        If Val(txtfirst.Text) Mod 2 = 0 Then
            num1 = Val(txtfirst.Text) + 1
        Else
            num1 = Val(txtfirst.Text)
        End If
        num2 = Val(txtlast.Text)
        Do While num1 < num2
            ListBox1.Items.Add(num1)
            num1 = num1 + 2
        Loop
      End Sub
2  .  Dim num1, num2 As Integer
        If Val(txtfirst.Text) Mod 2 = 0 Then
            num1 = Val(txtfirst.Text) + 1
        Else
            num1 = Val(txtfirst.Text)
        End If
        num2 = Val(txtlast.Text)
        Do Until num1 > num2
            ListBox1.Items.Add(num1)
            num1 = num1 + 2
  Loop
3  .  Dim num1, num2 As Integer
        If Val(txtfirst.Text) Mod 2 = 0 Then
            num1 = Val(txtfirst.Text) + 1
        Else
            num1 = Val(txtfirst.Text)
        End If
        num2 = Val(txtlast.Text)
        For num1 = num1 To num2 Step 2
            ListBox1.Items.Add(num1)
           
        Next




No comments:

Post a Comment

What & How