4 Replies - 2263 Views - Last Post: 22 July 2010 - 06:30 AM

#1 Nikhil_07n   User is offline

  • The cheese stands alone..
  • member icon

Reputation: 54
  • View blog
  • Posts: 2,490
  • Joined: 09-January 09

To check whether a number is Prime number or not

Posted 27 January 2009 - 08:27 PM

Description: Just paste the code in a form module with a command button 'command1'.Prime Number : Number not divisible by any number except 1 and itself.
'==========================Paste the following in a form module=============
'===========================================================================
Private Sub Command1_Click()
p = InputBox("enter a no.", "Checkin Prime number")   'Take a number from user
switch1 = False         

For i = 2 To p - 1              'Note : We are checking all numbers smaller then entered number except 1 and itself, which is , Obvious.

If p Mod i = 0 Then             'If the number gets divided by a number less then it leaving no remider, i.e., gets perfectly divided.
switch1 = True                  ' BANG : The alarm is on
End If

Next i                          

If switch1 = True Then          'If switch1 is true, i.e., gets perfectly divided by a number.
MsgBox ("Not a Prime Number")
Else                            'Else it will be a prime number
MsgBox ("A Prime Number")
End If

End Sub


'=============================END======================================


Is This A Good Question/Topic? 0
  • +

Replies To: To check whether a number is Prime number or not

#2 ultimitloozer   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 6
  • Joined: 27-June 08

Re: To check whether a number is Prime number or not

Posted 25 April 2009 - 07:10 PM

You can make this more efficient in several ways: 1) eliminate variant variables by specifically declaring a and p as long, switch1 as boolean, 2) check for even immediately after user input (if p mod 2 = 0 then switch1 = true else .... 3) make the loop as follows: for i = 3 to p \ 2 step 2 since we only need to check odd numbers and only up to half of the value input by the user, 4) terminate the loop immediately after a perfect divisor is found instead of continuing the loop.
Was This Post Helpful? 1
  • +
  • -

#3 arni0605   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 08-December 09

Re: To check whether a number is Prime number or not

Posted 20 December 2009 - 07:00 AM

thanks...great upload ...waiting for more fascinating programs
Was This Post Helpful? 0
  • +
  • -

#4 boka3090   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 08-January 10

Re: To check whether a number is Prime number or not

Posted 08 January 2010 - 05:04 AM

lot of thanks . thanks again . your program is so good and easier.
Was This Post Helpful? 0
  • +
  • -

#5 Rajani silwal   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 22-July 10

Re: To check whether a number is Prime number or not

Posted 22 July 2010 - 06:30 AM

thanks a lot for your kind help..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1