Check If A Given Number Is Prime Number in Python

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this Basic Python Program, , we will have a look at how to write a code in python to find if a given number is prime or not.

Python Program To Find If A Given Number Is Prime Number

num = 4
if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")
else:
   print(num,"is not a prime number")

Output:

4 is not a prime number

Leave a Comment

Your email address will not be published. Required fields are marked *