Python Program to Print all the Prime Number in an Interval
What is Prime numbers?
A prime number is referred to those numbers that can be divisible by them only. In simple words, those numbers can’t divisible by a number lower than them. For example, if we take 13 as input then we check that the 13 is divisible by 2 or not, if not then we check if the number is divisible by 3 or not, we check this till 12, if till 12 no one can’t be able to divide 13 completely so 13 is a prime number. If the 13 is divided completely by the number lower then it is not a prime number.
In the language of programming language, we will use a loop till 13 and check that 13 is divisible by the lower value or not. If it is divided by the lower value then we stop the execution and print that it is not a prime number else it is a prime number.
Numerical Example
Take a number=5
Now, we have to check the prime number then we simply start dividing 5 with 2 not with 1 because anything divided by 1 its remainder is 0.
Now, after dividing 5 by 2 we get 2 in dividend and 1 in the remainder,
Now, divide 5 by 3 we get 1 in dividend and 2 in the remainder,
Now, divide 5 by 4 we get 1 in dividend and 1 in the remainder.
Hence, 5 is not completely divisible till 5, so 5 is a prime number.
How to check the prime numbers in python?
Let’s Understand an Example with the help of the program.
#taking an input from the user
num = int(input(“Enter the number to check: “))
#Declaring a variable
#which is required for the program
flag=0
#executing the For loop
for no in range(2,num):
#Checking for prime number
if (num%no)==0:
#Required for further checking
flag=1
#stop the execution of loop
break
#Checking the flag value
# for final declaration of prime number
if flag==1:
print(num,” is not a prime number”)
else:
print(num,” is a prime number”)