Python program to print all Prime numbers in an Interval

In this article, you will learn to print all the prime numbers between a given interval.

What is a Prime number?

A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole number that can be divided evenly into another number. 

The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.

Step to create these programs:

  1. First, we take the first number as 1
  2. Then we take the second number from the user
  3. Then we iterate the for loop from first to last+1
  4. Then we check the number is greater than 1 
  5. Then we iterate again for the second loop from 2 to num
  6. Then we check the num, divide the num from the for loop and the check is equal to zero
  7. Otherwise, print num

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Python program to display all the prime numbers within an interval
# prime number in python

# take input of lower and upper limit from the user
first= 1  
#Take the input from the user:  
last= int(input("Enter the number: "))  
  
for num in range(first,last+ 1):  
   if num > 1:  
       for i in range(2,num):  
           if (num % i) == 0:  
               break  
       else:  
           print(num)  

# prime number in python - docodehere.com

Output

Enter the number: 12 2 3 5 7 11
Next Post Previous Post
No Comment
Add Comment
comment url