Python program to print all Prime numbers in an Interval
Given two positive integer start and end. The task is to write a Python program toprint all Prime numbers in an Interval.
Definition: A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.
The idea to solve this problem is to iterate the val from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.
Below is the Python implementation:
# Python program to print all # prime number in an interval start = 11end = 25 for val in range(start, end + 1): # If num is divisible by any number # between 2 and val, it is not prime if val > 1: for n in range(2, val): if (val % n) == 0: break else: print(val) |
Output:
11 13 17 19 23
The above solution can be optimized using Sieve of Erathostenes. Please see print prime numbers in a range for details.
Recommended Posts:
- Python Program to Print Numbers in an Interval
- Program to find Prime Numbers Between given Interval
- Python Program for Efficient program to print all prime factors of a given number
- Print the nearest prime number formed by adding prime numbers to N
- Print prime numbers with prime sum of digits in an array
- Python program to print all even numbers in a range
- Python program to print even numbers in a list
- Python program to print odd numbers in a List
- Python program to print all odd numbers in a range
- Python program to print positive numbers in a list
- Python program to print all negative numbers in a range
- Python program to print all positive numbers in a range
- Python program to print negative numbers in a list
- Print prime numbers in a given range using C++ STL
- Print all prime numbers less than or equal to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



