DAY 11 of PYTHON top 100 questions : from Basic to Advanced !!

Write a Python program to check whether a given number is a prime number or not:-

num = int(input("Enter a number: "))

result = ""
i = 2

while i * i <= num:
    if num % i == 0:
        result = "not "
        break
    i += 1

if num < 2 or result == "not ":
    result = "not " + result

print(num, "is", result + "a prime number.")

output :


Enter a number: 5
5 is a prime number.

The code iterates through the numbers starting from 2 and checks if any of them evenly divide the input number. If a divisor is found, it means the number is not prime. Otherwise, the number is considered prime.


If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).


HAPPY LEARNING !!!

0
Subscribe to my newsletter

Read articles from Priya Chakraborty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Priya Chakraborty
Priya Chakraborty

Hello, I'm Priya Chakraborty, a dedicated B.Tech student in Electrical Engineering at Siliguri Institute Of Technology. I'm an enthusiastic learner, constantly seeking to expand my skill set and knowledge base. With a solid foundation in programming languages such as C, R, Python, and MySQL, I'm poised to tackle complex technical challenges. But my passions extend beyond the realm of engineering. I'm also an aspiring content writer, driven by a curiosity to communicate ideas, both technical and non-technical, in a way that captivates and educates. My journey is defined by a relentless pursuit of self-improvement, coupled with strong communication skills. I believe in the power of continuous learning and the importance of sharing knowledge.