VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Code to verify whether an entered number is a prime number or a composite number.

by Sameet Natekar (6 Submissions)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Fri 28th January 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Code to verify whether an entered number is a prime number or a composite number.

API Declarations


'The form must contain a textbox (Text1) and 2 command buttons(Command1, Command2) and a label (label1)

Rate Code to verify whether an entered number is a prime number or a composite number.



Dim x, i, pr As Integer
i = 2
pr = 1
x = Text1.Text
If x >= 2 Then
    For i = 2 To x - 1
        If x Mod i = 0 Then
            pr = pr * 0
        End If
    Next
    If pr = 0 Then
        Label1.Caption = x & " is a Composite number."
    Else
        Label1.Caption = x & " is a Prime number."
    End If
Else
    If x = 1 Then
        Label1.Caption = "1 is niether a prime nor composite number. It's a unique number."
    End If
End If
Text1.SetFocus
End Sub

Private Sub Command2_Click()
Text1.Text = ""
Label1.Caption = ""
Text1.SetFocus
End Sub

Private Sub Text1_Change()
n = Len(Text1.Text)
e = 1
For i = 1 To n
If Mid(Text1.Text, i, 1) >= 0 And Mid(Text1.Text, i, 1) <= 9 Then
e = e * 1
Else
e = e * 0
End If
Next
If e = 0 Or Text1.Text = "" Then
Command1.Enabled = False
Else
Command1.Enabled = True
End If
End Sub

Download this snippet    Add to My Saved Code

Code to verify whether an entered number is a prime number or a composite number. Comments

No comments have been posted about Code to verify whether an entered number is a prime number or a composite number.. Why not be the first to post a comment about Code to verify whether an entered number is a prime number or a composite number..

Post your comment

Subject:
Message:
0/1000 characters