Python Program to Print Prime Numbers
In this article, I've included multiple programs in Python, that prints prime numbers. Here are the list of programs
covered in this article:
- Print prime numbers from 1 to 100 in Python
- Print prime numbers from 1 to 100 in single line
- Print prime numbers upto n in Python
- Print prime numbers in a given range in Python
- Print prime numbers in a given range, using while loop
Print Prime Numbers from 1 to 100 in Python
The question is, write a Python program to print all prime numbers from 1 to 100. The program given below is
its answer:
print("----Prime Numbers from 1 to 100----")
start = 2
end = 100
for i in range(start, end+1):
count = 0
for j in range(2, i):
if i%j == 0:
count = 1
break
if count == 0:
print(i)
The snapshot given below shows the sample output produced by above Python program, that prints all prime numbers
from 1 to 100:
Because 1 is neither a prime number nor a composite number. Therefore, I've started with 2, rather with 1.
Print Prime Numbers from 1 to 100 in Single Line
If you want to print all prime numbers from 1 to 100 in a single line, then the only replacement you need to
do, from above program is, replace the following statement:
with the statement given below:
Now the output will be, after doing above replacement and re-executing the previous program:
----Prime Numbers from 1 to 100----
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Print Prime Numbers upto n in Python
The question is, write a program in Python to print prime numbers upto n. The value of n must be
received by user at run-time of the program. The program given below is its answer:
print("Enter the Value of n: ", end="")
n = int(input())
start = 2
print("\n----Prime Numbers upto", n, "----")
for i in range(start, n+1):
count = 0
for j in range(2, i):
if i%j == 0:
count = 1
break
if count == 0:
print(i, end=" ")
Sample run of above Python program, with user input 50 to print prime numbers upto 50, is shown in the snapshot
given below:
Display Prime Numbers in a Given Range in Python
Here is another program on printing of prime numbers, in a given range, or an interval, specified by user at run-time:
print("---Enter the Range---")
print("Enter Starting Number: ", end="")
start = int(input())
print("Enter Ending Number: ", end="")
end = int(input())
print("\n----Prime Numbers from", start, "to", end, "----")
for i in range(start, end+1):
count = 0
for j in range(2, i):
if i%j == 0:
count = 1
break
if count == 0:
print(i, end=" ")
The snapshot given below shows the sample run of above Python program, with user input 100 and 120
as range to print all prime numbers from 100 to 120
Print Prime Numbers using while Loop in Python
Now let me create the same program as of previous, but using while loop this time, instead of for loop
print("---Enter the Range---")
print("Enter Starting Number: ", end="")
start = int(input())
print("Enter Ending Number: ", end="")
end = int(input())
print("\n----Prime Numbers from", start, "to", end, "----")
i = start
while i < end+1:
count = 0
j = 2
while j < i:
if i%j == 0:
count = 1
break
j = j+1
if count == 0:
print(i, end=" ")
i = i+1
Same Program in Other Languages
Python Online Test
« Previous Program
Next Program »