Write a Python program to accept two positive integers x and y from the user and then find and print all the prime numbers between x and y. 

Answer:

Answer by student

x = int(input( "Enter the lower limit: " ))

y = int(input( "Enter the upper limit: " ))

 

print( "The prime numbers between" , x, "and" , y, "are:" )

 

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

  if num > 1 :

    is_prime = True

    for i in range( 2 , num):

      if num % i == 0 :

        is_prime = False

        break

    if is_prime:

      print(num)

Detailed answer by teachoo

-lock-

  • The Python program to find and print all the prime numbers between x and y consists of the following steps:
    • First, we accept two positive integers x and y from the user using the input() function and convert them to integers using the int() function. We assign them to the variables x and y respectively. 
    • Next, we print a message using the print() function to indicate what we are going to do. We use string concatenation (+) and type conversion (str()) to display the values of x and y along with the message.
    • Then, we use a for loop to iterate over the range of numbers from x to y, including both. The range() function returns a sequence of numbers starting from the first argument (x) and ending at one less than the second argument (y + 1).We use the variable num to store each number in the range in each iteration of the loop.
    • Inside the loop, we check if num is greater than 1 using an if statement. This is because prime numbers are defined as natural numbers greater than 1. If num is less than or equal to 1, we skip it and move to the next number in the range.
    • If num is greater than 1, we assume that it is prime initially and assign a boolean value True to a variable called is_prime . We use this variable to keep track of whether num is prime or not.
    • Then, we use another for loop to iterate over the range of numbers from 2 to num-1. The reason for this is that we want to check if num has any positive divisors other than 1 and itself. We use the variable i to store each number in this range in each iteration of the loop.
    • Inside this loop, we check if num is divisible by i using the modulo operator (%). The modulo operator returns the remainder when one number is divided by another. If num is divisible by i , it means that it has a positive divisor other than 1 and itself, and hence it is not prime. In that case, we assign a boolean value False to the variable is_prime and break out of the loop using the break statement. The break statement terminates the current loop and resumes execution at the next statement after the loop.
    • After checking all the possible divisors of num , we check if is_prime is still True using another if statement. If it is True, it means that num has no positive divisors other than 1 and itself, and hence it is prime. In that case, we print num using the print() function.

So, the final code is:

# accept two positive integers x and y from the user

x = int(input( "Enter the lower limit: " ))

y = int(input( "Enter the upper limit: " ))

 

# print a message

print( "The prime numbers between" , x, "and" , y, "are:" )

 

# loop from x to y

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

  # check if num is greater than 1

  if num > 1 :

    # assume num is prime initially

    is_prime = True

    # loop from 2 to num-1

    for i in range( 2 , num):

      # check if num is divisible by i

      if num % i == 0 :

        # num is not prime, break the loop

        is_prime = False

        break

    # if num is prime, print it

    if is_prime:

      print(num)

-endlock-

Remove Ads
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.