Hellow friends, please help me to create a program that will disolay only a postive prime number

Dani AI

Generated

asked for a VB6 program that shows only positive prime numbers. A positive prime is an integer greater than 1 whose only divisors are 1 and itself (2 is the only even prime). A compact, correct approach is to validate input, use a 32-bit Long for numeric work (VB6 Integer is only 16-bit), then test divisibility up to the square root of the candidate while skipping even divisors.

A minimal IsPrime routine and a typical form handler follow. The function uses O(sqrt(n)) time and is safe for ordinary desktop ranges.

Function IsPrime(ByVal n As Long) As Boolean
    Dim i As Long
    If n < 2 Then
        IsPrime = False
        Exit Function
    End If
    If n = 2 Then
        IsPrime = True
        Exit Function
    End If
    If n Mod 2 = 0 Then
        IsPrime = False
        Exit Function
    End If
    For i = 3 To CLng(Sqr(n)) Step 2
        If n Mod i = 0 Then
            IsPrime = False
            Exit Function
        End If
    Next i
    IsPrime = True
End Function

Private Sub Command1_Click()
    Dim s As String
    s = Trim(Text1.Text)
    If Not IsNumeric(s) Then
        Label1.Caption = "Enter a whole positive number"
        Exit Sub
    End If
    Dim n As Long
    n = CLng(Val(s))
    If IsPrime(n) Then
        Label1.Caption = n & " is prime"
    Else
        Label1.Caption = n & " is not prime"
    End If
End Sub

Notes and troubleshooting: for producing many primes up to N, use a Sieve of Eratosthenes instead of repeated IsPrime calls; very large N can hit VB6 memory and Long limits, so consider a modern runtime or specialized library for big integers. The advice from and to show an attempted solution is sensible; the snippet above is a reliable starting point that can be adapted to either validate single inputs or generate a list of primes.

Recommended Answers

All 2 Replies

Sorry, we don't just hand you code on request. If you've already tried something and it doesn't work, post that and we can help you fix it.

hi isaya, how far you doing this?
post your code and we will trying to help you,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.