Program to check if the given number is prime number in Python

by | Jan 19, 2021 | Python Programs

Home » Python » Python Programs » Program to check if the given number is prime number in Python

Introduction

In this section of python programing, we will understand the python to check whether the given number is prime or not. A prime no is the positive integer which is divisible by 1 or number itself.

 

Program

num = int(input("Please enter the number to check: "))
if num > 1:
    for i in range(2, num):
        if (num%i) == 0:
            print("The number is not prime!")
            break
    else:
        print("The number is prime number")
else:
    print("The number is not prime!")

 

Output

Explanation

In the above python code we have created a variable num which stores integer input from user. After that if condition checks whether the input is positive integer of or not and in case the condition satisfies the for loop iterates from 2 to num/2. If the num given is divided by any other number then the number is not prime otherwise the number is prime.

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author