Python program to check whether a number is Prime or not

 In this article, you will learn to find whether the number is a prime number or not.

What is a Prime number?

prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole number that can be divided evenly into another number. 

The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# prime number in python
# first we will take input from the user to check is prime number or not
num = int(input("Enter number to check: "))

# Prime number is always greater than 1
if num > 1:

# now divide the number by 2 to number itself 
    for i in range(2, int(num/2)+1):
        # And check the reminder is zero
        if (num % i) == 0:
            # if reminder is zero than the number is not a prime number
            print(num, "is not a prime number")
            break
    else:
        print(num, "is a prime number")
  
else:
    print(num, "is not a prime number")

# prime number in python - docodehere.com



Output


Enter number to check: 23 23 is a prime number


Next Post Previous Post
No Comment
Add Comment
comment url