Python Program to Check Prime Numbers – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Check Prime Numbers.

Code

# Python Program to Check Prime Numbers

num = int(input("Enter a positive number: "))

# define a flag variable
flag = False

# prime numbers are greater than 1
if num > 1:
    # check for factors
    for i in range(2, num):
        if (num % i) == 0:
            # if factor is found, set flag to True
            flag = True
            # break out of loop
            break

# check if flag is True
if flag:
    print(num, "is not a prime number")
else:
    print(num, "is a prime number")

Output

Enter a positive number: 23
23 is a prime number
Enter a positive number: 12
12 is not a prime number
Enter a positive number: 11
11 is a prime number

Check out our other python programming examples

Leave a Comment

Your email address will not be published.