Python Program to Print Prime Numbers In a Given Range

Find the Prime Numbers in a Given Range in Python

Find the Prime Numbers in a Given Range in Python

Given two integer as Limits, low and high, the objective is to write a code to in Python Find Prime Numbers in a Given Range in Python Language. To do so we’ll use nested loops to check for the Prime while Iterating through the range.
Example
Input : low = 2 , high = 10
Output : 2 3 5 7

Find the Prime Numbers in a Given Interval in Python

Given two integer variables for range, the objective is to check for all the prime number that lay in the given interval. The two input integers will act as the interval limits low and high. In order to check which iterating, we’ll use nested loops. The outer loop will iterate through the numbers while the inner loop will check for Prime. Here are some of the methods used to solve the above mentioned problem in python language

We’ll discuss the above mentioned methods in the upcoming sections below. Check out Python Program to check for Prime. Check out the definition in the blue box below.

Method 1: Using inner loop Range as [2, number-1]

Working

For two integer inputs as low and high limits of the interval,

  • Run a for loop to iterate through all the numbers.
  • Run a Nested for loop to check for prime or not.

Let’s implement the above logic in Python Language. Lets see the implementation now for python program to find prime numbers in range

Python Code

Run
# python find prime numbers in range 
low, high = 2, 10
primes = []

for i in range(low, high + 1):
    flag = 0

    if i < 2:
        continue
    if i == 2:
        primes.append(2)
        continue

    for x in range(2, i):
        if i % x == 0:
            flag = 1
            break

    if flag == 0:
        primes.append(i)
        
print(primes)

Output

[2, 3, 5, 7]

Method 2: Using inner loop Range as [2, number/2]

Working

For two integer inputs as limits, we perform the following main operations,

  • Run a for loop to iterate through the numbers in a given interval.
  • Run a nested while to check for prime by checking if the number has any other factors in the range [2, number/2].

Let’s implement the above logic in Python Language.

Python Code

Run
low, high = 2, 10
primes = [2]

for num in range(low, high + 1):
    flag = 0
    if num < 2:
        flag = 1
        
    if num % 2 == 0:
        continue
    iter = 2

    while iter < int(num / 2):
        if num % iter == 0:
            flag = 1
            break
        iter += 1

    if flag == 0:
        primes.append(num)

print(primes)

Output

[2, 3, 5, 7]

Method 3: Using inner loop Range as [2, sqrt(number)]

Working

For two integer inputs as low and high limits of the interval, we perform the following

  • Run a for loop to iterate through the number in the given interval.
  • Run a nested while loop to check for prime or not.
  • We do so by checking if the number has any factors in the range [2, sqrt(number)].

Let’s implement the above logic in Python Language.

Python Code

Run
low, high = 2, 10
primes = [2, 3]

for num in range(low, high + 1):
    flag = 0
    
    if num < 2:
        flag = 1
        
    if num % 2 == 0:
        continue
        
    if num % 3 == 0:
        continue
        
    iter = 2
    while iter < int(pow(num, 0.5)):
        if num % iter == 0:
            flag = 1
            break
        iter += 1
        
    if flag == 0:
        primes.append(num)

print(primes)

Output

[2, 3, 5, 7]

Method 4: Using inner loop Range as [3, sqrt(number), 2]

Working

This method is similar to the one above but here we’ll use 2 as a step to skip the even numbers.

For a given interval as [low, high], we do the following

  • Run a for loop to iterate through the numbers that lay in the input interval.
  • Run a nest while eith step size as 2 from 3 to the square root of number and check for factors of the number in that interval.

Let’s implement the above logic in Python Language.

Python Code

Run
low, high = 2, 10
primes = [2, 3]

for num in range(low, high + 1):
    flag = 0
    if num < 2:
        flag = 1

    if num % 2 == 0:
        continue
        
    if num % 3 == 0:
        continue

    iter = 3

    while iter < int(pow(num, 0.5)):
        if num % iter == 0:
            flag = 1
            break
        iter += 2

    if flag == 0:
        primes.append(num)

print(primes)

Output

[2, 3, 5, 7]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

23 comments on “Python Program to Print Prime Numbers In a Given Range”


  • Sangram

    # Python Program to Print Prime Numbers In a Given Range
    def GivenRPrimeN(x, y):
    ”’This fx will give you prime numbers in given range by user”’
    lis = []
    for i in range(x, y):
    flag = 0
    for j in range(2, i):
    if i%j ==0:
    flag=1
    break
    if flag == 1:
    pass
    else:
    lis.append(i)

    return lis

    if __name__ == “__main__”:
    print(GivenRPrimeN(0, 150))


  • Raghvendra Singh Shekhawat

    N1 = int(input(“Enter the lower number: “))
    N2 = int(input(“Enter the higher number: “))

    for i in range(N1,N2+1):
    flag = 0
    for j in range(2,i):
    if i % j == 0:
    flag = 1
    break
    if flag == 0:
    print(i, end=” “)


  • praduman

    # Find the Prime Numbers in a Given Range in Python

    n,m=map(int,input().split())
    list=[]

    for i in range(n,m+1):
    p=0
    if i<2:
    continue
    for j in range(2,i):
    if i%j==0:
    p=1
    break
    if p==0:
    list.append(i)

    print(list)


  • Mohammed

    def primecheck(num):
    flag=False
    if num>1:
    for i in range(2,num):
    if num % i == 0:
    flag=True
    break
    else:
    return False
    if flag:
    return False
    else:
    return True

    lower=int(input(“Enter the lower limit: “))
    upper=int(input(“Enter the upper limit: “))
    arr=[]
    for i in range(lower,upper+1):
    if primecheck(i) is True:
    arr.append(i)
    print(arr)


  • Bharath

    lower = int(input(‘Enter a number: ‘))
    upper = int(input(‘Enter a number:’))

    print(“Prime numbers between”, lower, “and”, upper, “are:”)

    for num in range(lower, upper + 1):
    # all prime numbers are greater than 1
    if num > 1:
    for i in range(2, num):
    if (num % i) == 0:
    break
    else:
    print(num)


  • dwivedianimesh71

    m = int(input())
    n = int(input())
    for num in range(m,n + 1):
    if num > 1:
    for i in range(2,num):
    if (num % i) == 0:
    break
    else:
    print(num)