Find prime numbers in range Python
In this post, you will learn different ways to print prime numbers in a range using the Python programming language.
A prime number is a whole number greater than 1 whose only factors are 1 and itself, like -2, 3, 5, 7, 11, etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9, and the number itself.
Python find prime numbers in range using for loop
Here is the Python program to print prime numbers within a given interval using a for loop. Numbers less than or equal to 1 are not prime numbers. That's why we have started the range from 1. First, we loop over the ranges till the upper limit. The if statement checks for the divisors of the number. If the remainder is not equal to 0, it considers the number as prime.
for num in range(1, 50):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Output of the above code:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
Python find prime numbers in range using while loop
In the given Python program, we have used the while loop to check the prime numbers. The while loop is executed till the i variable is less than the num variable. Within the loop, we have used another while loop. This loop executes till the num divisible by 2 remains greater than equal to i. Within this, we have used an if statement to check the modulation value of the number divided by the i variable. If the modulation value is 0, then increment the counter value and a break statement moves the control out of the loop. Otherwise, control goes to another if statement, check condition. If it returns true, it prints the number as prime.
num = 1
while(num <= 25):
count = 0
i = 2
while(i <= num//2):
if(num % i == 0):
count = count + 1
break
i = i + 1
if (count == 0 and num!= 1):
print(" %d" %num, end = ' ')
num = num + 1
Output of the above code:
2 3 5 7 11 13 17 19 23
Python find prime numbers in range using user inputs
The program takes input from the user. The user is given two integer numbers, lower value, and an upper value. We loop through all the elements in the given range and check for each number if it has any factor between 1 and itself. If yes, then the number is not prime, and it will move to the next number. Otherwise, it prints the number as prime and moves to the next number in the range.
min_value = int(input ("Please enter the min value: "))
max_value = int(input ("Please enter the max value: "))
print("Prime Numbers in the given range: ")
for num in range (min_value, max_value + 1):
if num > 1:
for i in range (2, num):
if(num % i) == 0:
break
else:
print(num)
Output of the above code:
Please enter the min value:
1
Please enter the max value:
50
Prime Numbers in the given range:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
Python find prime numbers in range using function
In the given Python program, we define a function is_prime() to check whether a given number is prime or not. We loop from min_value to max_value and pass each number to the is_prime() function. If this function returns True, then we print it.
# Function to check prime numbers
def is_prime(num):
if num < 2:
return False
for i in range(2,num):
if num%i == 0:
return False
return True
# Getting user inputs
min_value = int(input('Enter minimum value: '))
max_value = int(input('Enter maximum value: '))
print('Prime numbers from %d to %d:' %(min_value, max_value))
for i in range(min_value, max_value+1):
if is_prime(i):
print(i, end=' ')
Output of the above code:
Enter minimum value:
10
Enter maximum value:
50
Prime numbers from 10 to 50:
11 13 17 19 23 29 31 37 41 43 47
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary