Python Program to Print All Prime Numbers in an Interval
Python Program to Print All Prime Numbers in an Interval
PYTHON EXAMPLES
Python Program to Print all Prime Numbers in an
Interval
Check if a Number is Positive, Negative
or 0 In this program, you'll learn to print all prime numbers within an interval using for loops
and display it.
To understand this example, you should have the knowledge of following Python
Check Prime Number programming topics:
A positive integer greater than 1 which has no other factors except 1 and the number itself
is called a prime number.
2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime
(it is composite) since, 2 x 3 = 6 .
Source Code
script.py IPython Shell
1 # Python program to display all the prime numbers within an interval
2
3 # change the values of lower and upper for a different result
4 lower = 900
5 upper = 1000
6
7 # uncomment the following lines to take input from the user
8 #lower = int(input("Enter lower range: "))
Receive the latest tutorial 9 #upper = int(input("Enter upper range: "))
to improve your 10
11 print("Prime numbers between",lower,"and",upper,"are:")
programming skills.
12
13 for num in range(lower,upper + 1):
14 # prime numbers are greater than 1
Enter Email Address* Join 15 if num > 1:
16 for i in range(2,num):
17 if (num % i) == 0:
18 break
19 else:
20 print(num)
Run
Powered by DataCamp
Get Templates
(Free)
Find Templates,
Download Now
RECOMMENDED READINGS FreeTemplateFinder
Here, we store the interval as lower for lower interval and upper for upper interval, and nd
prime numbers in that range. Visit this page to understand the code to check for prime
numbers.
R Tutorials
Algorithms Tutorials