Python program for Prime Number
In this article, you will learn different Python programs to print Prime numbers.
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 number itself.
The following programs print a list of prime numbers between a given range (that is, numbers not divisible by something other than 1 or the number itself) or check the user input.
Python Program to print Prime Numbers between interval
Here is the Python program to print prime numbers between 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.
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 program to check prime number using for loop
Here, we have used a for..else statement to check the prime number of the given user input. If 'num' is greater than 1, the for loop is executed. This loop checks the numbers between 2 and the number entered by the user. Within this loop, we have used another if statement. If the condition (number % i) == 0 is true, it breaks out of the loop. Otherwise, it prints the number. The last else statement is executed when the number entered is less than 1.
num = int(input("Enter a number: "))
for x in range(1, num):
if x > 1:
for i in range(2,x):
if (x % i) == 0:
break
else:
print(x)
Output of the above code:
Enter a number: 27
2
3
5
7
11
13
17
19
23
Python program to print prime numbers using while loop
In the given Python program, we have used the while loop to print 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 number divisible by 2 remains greater than equal to i. Within this, we have used a 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 <= 18):
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 29 31 37
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