Python Program to Print Prime Numbers In a Given Range

Print Prime Numbers in a Given Range

In this Python Program, we will find the prime numbers in a given range from start to end. In this program, we will use 2 for loop, one within a given given range of numbers inserted by the user. And inside that for loop, in which we will find whether the given number is prime or not using if condition as well.
For Example:- 
Enter the first number: 11
Enter the Second Number: 20
Prime Number 11
Prime Number 13
Prime Number 17
Prime Number 19
Python Program to Print Prime Numbers In a Given Range

Working:

Step 1. Start

Step 2. Take two user input for “First” and “Second” Variable.

Step 3. We will use two for loops, One starting from first to second. and Second starting from 2 to (i//2).

Step 4. Inside for loop, we will check whether (num % i == 0) condition is satisfied or not.

Step 5. If condition is true, then nothing is printed.

Step 6. If it is false and break the for loop, then print, “Prime Number i.”

Step 7. Stop

Python Program:

first = int(input("Enter the first number:"))
second = int(input("Enter the Second Number:"))
for i in range(first, second):
    for j in range(2, i//2):
        if i % j == 0:
            break
    else:
        print("Prime Number", i)

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student) 

Output:

Enter the first number:11
Enter the Second Number:56
Prime Number 11
Prime Number 13
Prime Number 17
Prime Number 19
Prime Number 23
Prime Number 29
Prime Number 31
Prime Number 37
Prime Number 41
Prime Number 43
Prime Number 47
Prime Number 53

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


  • Rahul

    Above mentioned program gives wrong output
    The Correct Code is:
    f=int(input())
    s=int(input())
    for i in range(f,s):
    for j in range(2,i):
    if i%j == 0:
    break
    else:
    print(“Prime Number”, i)


  • Nikitha

    lower = int(input(“Enter the lower interval:”))
    upper = int(input(“Enter the upper interval:”))
    for num in range(lower,upper):
    num>1
    for i in range(2, num):
    if num % i == 0:
    break
    else:
    print(“Prime Number”, num)


  • Shubham

    This program provides the wrong output, The number 4 is not a prime number because it can be divided evenly by 4, 2, and 1.
    Enter the first number:1
    Enter the Second Number:25
    Prime Number 1
    Prime Number 2
    Prime Number 3
    Prime Number 4
    Prime Number 5
    Prime Number 7
    Prime Number 11
    Prime Number 13
    Prime Number 17
    Prime Number 19
    Prime Number 23