Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Program To Check Whether The Number Is Prime Or Not

2 min read

A prime number is a positive whole number greater than 1 which is evenly divisible only by 1 and itself. 2, 3, 5, 7, 11, 13 are the first few prime numbers.

Some interesting facts about Prime numbers -

  • 2 is the only even prime number
  • 0 and 1 are not considered as prime numbers
  • Numbers that have more than two factors are called composite numbers.
  • No prime number greater than 5 ends in a 5. Any number greater than 5 that ends in a 5 can be divided by 5.

In this article, we will create a Python program to check whether the given number is Prime or not.

Algorithm To Check Whether A Number Is Prime Or Not

Step 1: Take the input from User

Step 2: Check whether the number is greater than 1, if not than the number is not Prime

Step 3: Check if the number gets evenly divided by any number from 2 to half of the number

Step 4: Print the result

Here we have optimized the algorithm to search only till half of the given number which drastically improves the performance for a very large number.

Python Program To Check Whether The Number Is Prime Or Not

# Taking the input from User
number = int(input("Enter The Number"))
if number > 1:
    for i in range(2,int(number/2)+1):
        if (number % i == 0):
            print(number, "is not a Prime Number")
            break
    else:
        print(number,"is a Prime number")
# If the number is less than 1 it can't be Prime
else:
    print(number,"is not a Prime number")

Explanation

In the given program first, we are taking the input from the user using the input keyword and converting it to integer datatype in case the user inputs a Float number. Next, we are comparing if the number is less than 1 because only a number greater than 1 can be called a Prime number.

Inside the loop, we are diving the number to every number between 2 and the half of the number if no factor is found the number is Prime gets printed out along with the number.

Running the program for the following test cases gave us the expected result.

Enter The Number 2
2 is a Prime number
Enter The Number 11
11 is a Prime number
Enter The Number 9729262
9729262 is not a Prime Number
Enter The Number -20
-20 is not a Prime number
Enter The Number 1
1 is not a Prime number


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read