Prime Number Program in Python – Explained in Super easy Way

You must have heard this term in your school and college days.

This is also a favorite question to write a prime number program in college exams or in your interview.

Prime numbers are the positive integers having only two factors, 1 and the integer itself. For example, factors of 6 are 1,2,3, and 6, which are four factors in total. But factors of 7 are only 1 and 7, totally two.

Hence, 7 is a prime number but 6 is not, instead it is a composite number. But always remember that 1 is neither prime nor composite.

We can also say that the prime numbers are the numbers, which are only divisible by 1 or the number itself. Another way of defining it is a positive number or integer, which is not a product of any other two positive integers.

There is no defined formula to find if a number is prime or not (except to a certain range), apart from finding its factors.

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 could not be divided into groups of equal numbers.

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.

We want you to know about the Prime Number Program in Python, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

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 Explanation on Python please click over here. Python Practice Series.

Video Explanation to Prime Number Program in Python

Code: Prime Number Program in Python

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.

Let’s start with code.

Method 1: Prime Number Program in Python

Source Code and Output

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 Method 1: 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 have 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 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 value of input number, we will use 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 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 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.

Method 2: Prime Number Program using a for…else statement in Python

Source Code and Output

num = 407 
if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           break
   else:
       print(num,"is a prime number")
       
# if input number is less than
# or equal to 1, it is not prime
else:
   print(num,"is not a prime number")

Output:

407 is not a prime number

Code Explanation Method 2: Prime Number Program using a for…else statement in Python

In this program we have stored a number into variable num, you can even take user input here.

Here we will check if the number is greater than 1 or not, this is just because if the number is less than or equal to 1 then it is not a prime number.

Now we will check for factors, we have a for loop and i as a counter, and the loop will start from 2 and run till num-1 as in range the last number is opted out.

Then inside the loop we will check if the number is divisible by counter i or not, if it is divisible by counter then it is not a prime number and we will print num is not a prime number. And we will break the loop

Now we will write another statement, inside else we will write it is a prime number.

Outside the indent of the for loop, we have another statement for main where we have checked if num is greater than 1 if not then the number is not a prime number, which we have printed in the else statement.

Run the program and you will get 407 is not a prime number.

It works on the logic that the else clause of the for loop runs if and only if we don’t break out the for loop. That condition is met only when no factors are found, which means that the given number is prime.

Now you know what is a prime number and how to check if a number is a prime number or not.

Facebook Comments
(Visited 3 times, 1 visits today)

Leave A Comment

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