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:
- First, we take the first number as 1
- Then we take the second number from the user
- Then we iterate the for loop from first to last+1
- Then we check the number is greater than 1
- Then we iterate again for the second loop from 2 to num
- Then we check the num, divide the num from the for loop and the check is equal to zero
- 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