python program to check entered number is prime or not

In this example, We will take number as user input and check inputted number is Prime or not.

If any natural number is greater than 1 and having no positive divisors other than 1 and the number itself it's called prime number. For example: 3, 7, 11 etc are prime numbers.

Here, we will ask user for input and pass it to user define function that check number is prime or not.

Example :

def IsPrimeNumber(no):  
    if no > 1:  
        for j in range(2, int(no/2) + 1):  
            if (no % j) == 0:  
                print("Number is not a prime number")  
            print(no, "is a prime number")  
        print("Number is not a prime number")
no = int(input("Enter an input number:")) 

Output :

Enter an input number:51
Number is not a prime number

Explanation :

This program will store user input into number variable and pass it to IsPrimeNumber() then function it self check number is positive or not. If number is positive then it will loop from 2 to number itself if number can not divide by any numbers then it will print number is prime number.

Share your thoughts

Ask anything about this examples