Python Program for Prime numbers between two numbers

Get two inputs x and y and print all the prime numbers between x and y.

Sample Input 1 :

10 40

Sample Output 1 :

11 13 17 19 23 29 31 37

Program or Solution


#Python Program to print prime numbers between x and y

x = int(input("Enter a Number X:")) #get input x

y = int(input("Enter a Number Y:")) #get input y

#start your travel from x to y

for n in range(x,y+1):

#check which are the numbers from 2 to n/2 divides n.

#No other after n/2 divides n except n

    for i in range(2,n//2+1):

        if n % i == 0: #if divisible then it is not prime.


    else: #this is else of for  statement. executes after last iteration if loop is not broken at any iteration.

        print("{} ".format(n))

Program Explanation

Prime numbers are numbers those only divisible by 1 and same. using a outer for loop we check take every number n from x to y 

Inner for loop checks each number is prime or not, if prime it prints the  n.

Tech Events