Prime Number in Python

Prime number in python | A natural number which has only two factors ( 1 and itself ) is called a prime number. For example- 5 is a prime number because it has only two factors 1 and 5. Similarly, 9 is not a prime number because it has more than 2 factors that are 1,3, and 9.

Python Program to Check Prime Number

To develop a program to check the given number is a prime number or not in Python; first, you should know how to develop a Python program to find out all factors of a number. Because if any number has more than 2 factors then only, it is a prime number. All negative numbers, 0 and 1 are not the prime numbers.

Using For Loop

This python program using the for loop. We will take integer numbers while declaring the variables. Then, check given number is prime number or not using the for loop and finally, the result will be displayed on the screen.

# Python program to check if a number is prime or not

# take inputs
num = int(input('Enter a number: '))

# If number is greater than 1
if num > 1:
    for i in range(2, num//2):
        if (num % i) == 0:
            print(num, "is not a prime number")
            break
    else:
        print(num, "is a prime number")
else:
    print(num, "is not a prime number")

Output for the different input values:-

Enter a number: 5
5 is a prime number

Enter a number: 20
20 is not a prime number

Enter a number: 47
47 is a prime number

Prime Number Program in Python using While Loop

In the previous program, we will check prime number using for loop but in this program, check if a number is prime or not using the while loop.

# Python program to check if a number is prime or not

# take inputs
num = int(input('Enter a number: '))

count = 0
i = 2

# If number is greater than 1
while(i <= num//2):
    if(num % i ==0):
        count += 1
        break
    i += 1
 
# display result
if(count == 0 and num != 1):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

Output:-

Enter a number: 13
13 is a prime number

Prime Number in Python using Function

In this program, We can also take the help of a user-defined function to check if a number is prime or not. A function is a block of code that performs a specific task.

# Python program to check if a number is prime or not

def isPrime(num): #user-defined function
    if num > 1:
        for i in range(2, num//2):
            if (num % i) == 0:
                return False
                break
        else:
            return True
    else:
        return False

# take inputs
num = int(input('Enter a number: '))

# calling function and display result
if(isPrime(num)):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

Output:-

Enter a number: 250
250 is not a prime number

Program Using Recursion

We can also use the recursion technique to check if a number is prime or not in Python. A technique of defining the method/function that contains a call to itself is called recursion.

# Python program to check if a number is prime or not using recursion

def isPrime(num, i = 2):  #user-defined function
    if (num <= 2): 
        return True if(num == 2) else False
    if (num % i == 0): 
        return False
    if (i * i > num): 
        return True

    # Check next divisor 
    return isPrime(num, i + 1)
  
# take inputs
num = int(input('Enter a number: '))

# calling function and display result
if(isPrime(num)):
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")

Output:-

Enter a number: 11
11 is a prime number

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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