11/10/2011

How to check a prime number? Vb.Net code

Vb.net code for a checking a number whether it is prime or not is very simple just write following code on any event on which you want to check the number.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 be refer


  • First design a form where you can enter the number which you want to check. take following control on the form.
       label1.text               Text property  "Enter Number"
      Textbox1.text           Text property    ""
      Command button     Text property   "Check", name property "BtnCheck"

 2. Second Step: Write following code on click event of BtnCheck.
Private Sub BtnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCheck.Click
        Dim i, j As Integer
        Dim t As Boolean
        i = TextBox1.Text
        t = True
        For j = 2 To (i - 1)
            If i Mod j = 0 Then
                t = False
                Exit For
            End If
        Next j
        If t Then
            MsgBox(i & " is a prime Number")
        Else
            MsgBox(i & " is not a prime Number")
        End If
    End Sub.