Check If A Given Number Is Prime Number in Python

Share on facebook
Share on twitter
Share on pinterest

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

Subscribe to our Newsletter

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Share this post with your friends

Share on facebook
Share on google
Share on twitter
Share on linkedin

Leave a Reply

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