ScanSkill

Check Prime or Not in Python

In this, you'll get to learn how to check prime or not in Python. And the number is input from the user. i.e. Let's say if the user entered 4, then the output should be “it is not a Prime number”.

Prime numbers are -- 2, 3, 5, 7, 11, 13,...

This covers four different approaches:

  • Check prime or not in Python using for-loop
  • Check prime or not in Python using while loop
  • Check prime or not in Python using function
  • Check prime or not in Python using class

Prerequisites

Examples

Check prime or not in Python using for-loop

This is an example to check whether a number entered by the user is a prime number or not. In this for loop is used if the number is divisible by the numbers other small numbers other than 1.

print("Enter the Number: ")
num = int(input())

var1 = 0
for i in range(2, num):
    if num%i==0:
        var1 = 1
        break

if var1==0:
    print(f"{num} is a Prime Number")
else:
    print(f"{num} is not a Prime Number")

Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.

And, % (modulus) operator returns the decimal part(remainder) of the quotient.

Output:

Enter the Number: 
5
5 is a Prime Number
Enter the Number: 
4
4 is not a Prime Number

Check prime or not in Python using while loop

This example is to check whether a number entered by the user is a Prime number or not using while loop.

print("Enter the Number: ")
num = int(input())

var1 = 0
i = 2
while i<num:
    if num%i==0:
        var1 = 1
        break
    i = i+1

if var1==0:
    print(f"{num} is a Prime Number")
else:
    print(f"{num} is not a Prime Number")

Output:

Enter the Number: 
5
5 is a Prime Number
Enter the Number: 
4
4 is not a Prime Number

Check prime or not in Python using function

This example is to check prime or not in Python using the function method.

def checkPrimeorNot(n):
    for i in range(2, n):
        if n%i==0:
            return 1

print("Enter the Number: ")
num = int(input())

res = checkPrimeorNot(num)
if res==1:
    print(f"{num} is not a Prime Number")
else:
    print(f"{num} is a Prime Number")

Output:

Enter the Number: 
5
5 is a Prime Number
Enter the Number: 
4
4 is not a Prime Number

Check prime or not in Python using Class

This also does the same task as the above programs but using the Class method.

class PrimeNumber:
    def checkPrimeorNot(self, n):
        for i in range(2, n):
            if n%i==0:
                return 1

print("Enter the Number: ")
num = int(input())

prime1 = PrimeNumber()
res = prime1.checkPrimeorNot(num)

if res==1:
    print(f"{num} is not a Prime Number")
else:
    print(f"{num} is a Prime Number")

Here, all properties of the class named PrimeNumber get assigned to an object named prime1. Then the object can be used to access the member function of the class checkPrimeorNot using the dot (.) operator. (prime1.checkPrimeorNot(num))

Output:

Enter the Number: 
5
5 is a Prime Number
Enter the Number: 
4
4 is not a Prime Number

Conclusion

In this, we discussed different examples for different methods to find whether the number entered by the user is Prime or not -- using for loop, using while loop, using a function, and using class.