ADVERTISEMENT
ADVERTISEMENT

Python program to print all prime numbers in an interval

Here, we will write a Python program to print all prime numbers within the given interval.
Submitted by Shivang Yadav, on April 04, 2021

Prime Number is a natural number that is greater than 1 and cannot be formed by multiplying two smaller natural numbers.

We will take the intervals from the user and print all the prime numbers in the given range.

To print all prime numbers within an interval, we will loop over the interval and for each element check whether the element is a prime number or not. If it is prime, print it.

To check whether a number is prime or not, we need to check whether a number in the range(2, i) exists. If any such number exists, the given number is prime otherwise it is prime.

Program to print all prime number in an interval

# Python program to print all 
# prime numbers in an interval

# Getting input from users
lowerBound = int(input("Enter starting value of interval: "))
upperBound = int(input("Enter ending value of interval: "))

# Printing prime numbers within the range
print("All prime numbers in the interval are: ")
for number in range(lowerBound, upperBound+1):
  if number > 1:
    for i in range(2,number):
        if(number % i == 0):
            break
    else:
        print(number, end = "\t")

Output:

Enter starting value of interval: 5
Enter ending value of interval: 35
All prime numbers in the interval are:
5       7       11      13      17      19      23      29      31

Explanation:

In the above code, we have taken two inputs from the user, lowerBound and upperBound. And then printed all prime values in the range (lowerBound, upperBound).

Python Basic Programs »



ADVERTISEMENT


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.