Thursday, May 6, 2010

Prime Number or Not :-

Private Sub Check_PrimeorNot()

Dim N, D As Single
Dim tag As String
N = Cells(2, 2)

Select Case N
Case Is < 2 MsgBox "It is not a prime number" Case Is = 2 MsgBox "It is a prime number" Case Is > 2

D = 2
Do
If N / D = Int(N / D) Then
MsgBox "It is not a prime number"
tag = "Not Prime"
Exit Do
End If
D = D + 1
Loop While D <= N - 1 If tag <> "Not Prime" Then
MsgBox "It is a prime number"
End If
End Select
End Sub

No comments: