18/02/2012

How to find all the prime numbers between two given numbers? Vb.net code for beginners

     Like my earlier post , in this project I have used following Windows Controls 
  • Textbox- I have used to text boxes for getting the range of number from which we need to find the prime numbers.
  • Listbox- I have used one list box to show the result
  • One Button control to execute the Vb.net code on click event of button control
  • Required number of label control to name the various control
The Windows form which I have designed for this project is as under:

The code on click event of button control will be as under:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        Dim i, j As Integer
        For i = Val(TextBox1.Text) To Val(TextBox2.Text)
            For j = 2 To (i - 1)
                If i Mod j = 0 Then
                    Exit For
                End If

            Next j
            If i = j Then
                ListBox1.Items.Add(i)
            End If
        Next
    End Sub
For better understanding of these type of vb.net code or database related inquiry the book written by Mike Murach is very good book which is available on amazon with free shipping charge, I will suggest one can refer
Happy coding