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 the following Python 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
# Python program to display all the prime numbers within an interval lower = 900 upper = 1000 print("Prime numbers between", lower, "and", upper, "are:") for num in range(lower, upper + 1): # all prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num)
Output
Prime numbers between 900 and 1000 are: 907 911 919 929 937 941 947 953 967 971 977 983 991 997
Here, we store the interval as lower for lower interval and upper for upper interval, and find prime numbers in that range. Visit this page to learn how to check whether a number is prime or not.
Related posts:
Python Program to Access Index of a List Using for Loop
Python input()
Python Program to Find the Sum of Natural Numbers
Python Program to Find All File with .txt Extension Present Inside a Directory
Python String isprintable()
Python Sets
Python Program to Get the File Name From the File Path
Python String upper()
Python issubclass()
Python isinstance()
Python String translate()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Set isdisjoint()
Python Deep Learning Cookbook - Indra den Bakker
Python ascii()
Python Program to Capitalize the First Character of a String
Python Program to Check If a List is Empty
Python Program to Display Calendar
Python List reverse()
Python Program to Check if a Number is Positive, Negative or 0
Python Program to Count the Number of Each Vowel
Python Program to Iterate Through Two Lists in Parallel
Python Program to Illustrate Different Set Operations
Python int()
Python String expandtabs()
Python Program to Return Multiple Values From a Function
Python Set discard()
Python String zfill()
Python callable()
Python id()
Python String ljust()
Python Program to Find HCF or GCD