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

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

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