Ok i need the user to be asked for a number by an inputbox (Using VB 6) and have this number checked. If its prime i want them to be told via msgnox , if not i want them to be given another attempt

Any ideas?

You'll have to pick a maximum input number. Then work out all the prime numbers up to that number and put them in an array to check the user's input against. There isn't a shorthand method of finding out if a number is prime (though using base 6 can narrow the options down).

you think this woul work?

option explicit
 
dim prime as boolean
dim number as integer
dim x as integer
 
num = inputbox("Enter a whole number up to 100")
 
for x = 1 to 100
 
(code here)
 
next x

I just need the ocde here bit i think, but how do i get it to know whether a number is whole or not?

U can try this

Do while true

   Dim x as Long
   Dim myNum as long
   myNum = val(InputBox ("Enter A Number"))

   Dim mFlag as Byte
   mFlag = 0

   For x = 1 to Clng(myNum/2)
      If myNum Mod x = 0 then
         mflag = 1
         exit for
      end if
   next
   if mflag = 0 then
      Msgbox "Prime Number"
      Exit sub
   end if
loop

This works in an infinite loop, asking the user for a number, until the user has entered a prime number. To ensure it is a whole number, I typecasted it to a long. I avoided integer in case the user enters a number greater than 32676 (max value for an integer, IIRC). In this was you can also avoid having a maximum limit for the user input

Sorry for the howler in the code. The For Loop should be

For x = 2 to Clng(myNum/2)
....

The above code is a great example, but there is a way to make it faster. Consider the following:

Input number: 100
100 / 2:      50
Sqr(100):     10

Once you hit the square root of a number, you can stop, as it is likely to be a great deal smaller than the same number halved. Here's how I would do this (this is in fact an update of the program I used to learn QBASIC, which some of you may remember from the days of MS-DOS):

First, the IsPrime Function:

Function IsPrime(checkValue As Long) As Boolean
     For testValue = 2 To CLng(Sqr(checkValue)) Step 1
         If checkValue Mod testValue = 0 Then
             IsPrime = False
             Exit Function
         End If
     Next
     IsPrime = True
 End Function

And now for the code which will use the IsPrime function to solve your problem:

Sub Main()
    Do
        userValue = CLng(InputBox("Enter a value to test.  I will tell you if it is prime or not.", "Prime Tester", userValue))
        If IsPrime(userValue) Then
             If MsgBox(userValue & " is a prime number!  Congratulations!  Would you like to try another?", vbYesNo, "Number is Prime!") = vbNo Then
                End
            End If
        End If
    Loop
End Sub

Just for kicks and giggles (and since the program I'm redoing to bring you this solution did it this way too), I'll provide the code to list every number LESS THAN the user's input that is a prime number, taking advantage of the IsPrime Function again:

Sub Main()
    Do
        userValue = CLng(InputBox("Enter a value to test.  I will tell you every prime up to (and possibly including) it.", "Prime Tester", userValue))
        primesList = "1"
        For countValue = 2 To CLng(userValue) Step 1
                If IsPrime(CLng(countValue)) Then
                    primesList = primesList & ", " & CStr(countValue)
                End If
        Next
        If MsgBox("The prime numbers up to (and possibly including) " & userValue & " are:" & vbCrLf & primesList & vbCrLf & vbCrLf & "Would you like to try another?", vbYesNo, "Your Prime Numbers") = vbNo Then
            End
        End If
    Loop
End Sub

Other variations include the smallest prime >= <input number>, the largest prime <= <input number>, the first <input number> prime numbers, or any number of other things you might use the IsPrime function to do. This is a (relatively) quick function, and takes a mere fraction of the time you would use checking every number up to <input number> / 2 - especially for larger numbers.

As always, enjoy!

- Sendoshin

yes, checking till Square root is quicker

thats great it runs fine here in vb6. couldnt get it to run in net but fixed that, its just the sqr function is in system.math instead

Thanks for helping me i really needed to do it for school

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.