prime number or not

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shahrukh
    New Member
    • Sep 2006
    • 4

    prime number or not

    i have to determine the number entered by the user is a prime number or not. plz if you can tell me than reply.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by shahrukh
    i have to determine the number entered by the user is a prime number or not. plz if you can tell me than reply.
    A prime number is 1 that has no integer divisors other than 1 and itself.

    Once they have entered the number all you have to do is test that number to see if has any integer divisors other than 1 and itself.

    Start a loop with an initial value of 2 and an initial maximum of the number.
    Check to see if the number is exactly divisible by the loop control using % operator (number % control == 0), if it is the number is not prime
    If the number is not prime then set your maximum to number / control. This will reduce the number of values you have to check.

    If the loop ends without detecting the the number is not prime then the number is prime.

    Comment

    Working...