Skip to main content

Command Palette

Search for a command to run...

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

Published
2 min read
DAY 12 of PYTHON top 100 questions : from Basic to Advanced !!
P

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.

Write a Python program to Check whether the given number is prime or not.


n=int(input("Enter the number: "))

if n < 2 :
    is_prime= False
else:
    is_prime= True
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            is_prime= False
            break

if is_prime:
    print("YES,Number is Prime Number")
else :
    print('NO,Number is not Prime Number')

Output :

Enter the number: 8
NO,Number is not Prime Number

Enter the number: 7
YES,Number is Prime Number

The program prompts the user to enter a number, which is stored in the variable n.

Then, it checks if the number is less than 2. If it is, the variable is_prime is set to False since prime numbers are defined as integers greater than 1.

If the number is 2 or greater, the variable is_prime is initially set to True. It then proceeds to iterate from 2 to the square root of the number (inclusive) using a for loop. Within the loop, it checks if the number is divisible by the current divisor (i). If it is, the variable is_prime is set to False, indicating that the number is not prime, and the loop is terminated using break.

Finally, outside the loop, the program checks the value of is_prime. If it is True, it prints that the number is a prime number. Otherwise, it prints that the number is not a prime number.


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 !!!

More from this blog

Priya's blog

46 posts

Python Developer@Codeclause Mentee @Trailhead Salesforce Content Executive @GDSC SIT'22-23 Google Women Techmakers Ambassador @Google WTM Coreteam @ GirlScript Kolkata