Skip to content
Home » Program to Check Given number is a Prime Number

Program to Check Given number is a Prime Number

If a natural number is greater than 1 and has no positive divisors other than 1 and the number itself, it is said to be a prime number.

# A function for Prime checking conditions  
def PrimeorNot(num):  
    # Checking that given number is more than 1  
    if num > 1:  
        # Iterating over for loop  
        for i in range(2, int(num/2) + 1):  
            # If the given number is divisible or not  
            if (num % i) == 0:  
                print(num, " is not a prime number")  
                break  
        # Else it is a prime number  
        else:  
            print(num, "is a prime number")  
    # If the given number is 1  
    else:  
        print(num, "is not a prime number")  
# Get an input number from the user  
num = int(input("Enter an input number:"))  
# Calling function 
PrimeorNot(num)  

Output:

Enter an input number:5
5 is a prime number

Also Read:

How to Read password in encoded way Python