Prime Number Program in Python

Prime Number Program in Python
(Last Updated On: 13/09/2022)

What is a Prime Number?

A number greater than 1 with exactly two factors, i.e. 1, and the number itself is defined as a prime number. In other words, if a number cannot be divided into equal groups, then it is a prime number. We can divide a number into groups with equal numbers of items/elements only if it can be factorized as a product of two numbers. For example, 7 cannot be divided into groups of equal numbers.

This is because 7 can only be factorized as follows:

  • 7 × 1 = 7
  • 1 × 7 = 7

This means 1 and 7 are the only factors of 7. So, 7 is a prime number because it cannot be divided into groups of equal numbers. If you want to see all the practice examples and Explanations of Python, then please use this reference to this URL. 

Also, See – The Complete Python Practice Series.

Program to Check Prime Number in Python

n = int(input("Enter any number:"))
if n == 0 or n == 1:
    print("It is neither prime not composite number.")
elif n == 2:
    print("It is prime number.")
else:
    flag = True
    for i in range(2,n//2):
        if n%i == 0:
            flag = False
            break
    if flag == True:
        print("It is prime number.")
    else:
        print("It is not a prime number.")
Output:
Enter any number:11
It is prime number.

Code Explanation: Prime Number Program in Python

Here we have accepted a number from the user and converted it into an integer and stored it into variable n. Then we checked if n is zero or 1; if it is, then the number is not a prime number. Because a prime number is a whole number greater than 1. In the next line, we have checked if n is 2 or not. If it is then we will print the number as the prime number.  As per properties of the prime number, there is only one even prime number, that is, 2.

Then we have the else section; inside the else block, we have declared a flag variable with the value true. In the next line, we have a for loop with i as a counter variable and the loop will start from 2 to half the value of the input number; we will use the floor operator for this.

The floor operator (//) is used to return the closest integer value which is less than or equal to a specified expression or value. Inside the for loop, we have an if condition to check whether n is divisible by i or not, if it is, then we will set a flag to false and break this for loop.

Then outside of the for loop, we will check the flag value. If the flag is set to true then the number is a prime number else it is not a prime number. Let’s enter 11 and run the program, see this is a prime number.

Definition of a Prime Number

Any whole number greater than 1 that is divisible only by 1 and itself is defined as a prime number.

Do you still face difficulties in understanding the logic? That’s perfectly fine; we have something more for you. You can scroll down to learn more about this topic.

For all the practice Videos and Explanations on Python, please click here – Python Practice Series.

Properties of Prime Numbers

Some of the important properties of prime numbers are given below:

  • A prime number is a whole number greater than 1.
  • It has exactly two factors, that is, 1 and the number itself.
  • There is only one even prime number, that is, 2.
  • Any two prime numbers are always coprime to each other.
  • Every number can be expressed as the product of prime numbers.

Video Explanation of Prime Number Program in Python