Reading Time: 1 minutes
Prime numbers within a range in Python
A program to print Prime Numbers in an interval of 0 to the number entered by the user.
Prime numbers are integers greater than one with no positive divisors besides 1 and itself. Negative numbers are excluded.
## A program to print prime numbers in an interval of 0 to the number entered by the user. ## Primes are integers greater than one with no positive divisors besides 1 and itself. Negative numbers are excluded. upperBound = int(input("Please enter the upper bound of range:")) for number in range(1,upperBound + 1): # testing all numbers in range 1 to upperBound if number > 1: # prime numbers are greater than 1, the smallest prime number is 2. for i in range(2,number): # testing all numbers in range 2 to (number - 1) if (number % i) == 0: # if the number has any factor other than itself, then break out of the inner for loop break else: # if the number doesn't have any factor other than itself, print it as a prime. print(number) break
Try it online here.