How to verify a prime number in VB

See this post in: Chinese Version or French Version

Private Sub Command1_Click()
 r = InputBox(“Enter a number please”) ‘ Enter a number to verify
 For i = 2 To r
   If Val(r) Mod i = 0 And i <> Val(r) Then ‘ this phase give a condition which is not a prime number
    MsgBox (“The numbered you entered is not a prime number”)
    Exit Sub
   End If
 Next i
 MsgBox (“The numbered you entered is not a prime number”)
End Sub


‘A list of number from 1 to 100
Private Sub Command2_Click()
 r = 100
 Me.Caption = 0
 Me.ForeColor = vbBlue
 For j = 1 To r
   For i = 2 To j
     If Val(j) Mod i = 0 And i <> Val(j) Then
      Me.ForeColor = vbBlue ‘color of a not prime number
      Exit For
     Else
      Me.ForeColor = vbRed ‘color of a prime number
     End If
   Next i
  Print ” ” & Format(j, “000”);
  If Me.ForeColor = vbRed Then Me.Caption = Me.Caption + 1 ‘total of the prime number
  If j Mod 10 = 0 Then Print
 Next j
End Sub

Leave a Reply

↑ Back to top