In this Python programming tutorial, we will see a Python program to print prime numbers. Also, we will see, how to print the first 10 prime numbers in Python using a while loop. We will also see a few examples of how to print prime numbers from 1 to 100 in Python using a while loop and how to print first n prime numbers in Python using a while loop.
Python program to print prime numbers
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Here is the complete program to print all prime numbers in an interval.
We will start by getting the starting and ending values of the range from the user.
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
Then, to find whether a number is prime, we start by assuming it is a prime number. Then we try dividing it by all numbers from 2 up to the square root of the number. If any of these divisions result in a whole number, then the number is not a prime number.
Complete program:
start = int(input("Enter the start of range: "))
end = int(input("Enter the end of range: "))
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if (num % i) == 0:
break
else:
print(num)
Here’s how the program works:
- The user inputs the
start
andend
of the range. - We iterate through each number in the range using a
for
loop. - For each number, if it is greater than 1, we check for factors from 2 up to the square root of the number.
- If the number is exactly divisible (i.e., the remainder of the division is 0), then it is not a prime number and we
break
out of the innerfor
loop. - If no factor is found, the
else
block of the innerfor
loop is executed. Theelse
associated with thefor
loop only runs when the loop has finished normally (i.e., it didn’t encounter abreak
). - In this case, it prints the number because the number is a prime number.
Let’s say you want to find all prime numbers in the range 10 to 30. Here is how you would do it:
Enter the start of range: 10
Enter the end of range: 30
The output will be:
11
13
17
19
23
29
You can see the program output when I run the code.
This is how to write a Python program to print prime numbers.
Python program to print prime numbers using a while loop
Now, let us check, how to write a Python program to print prime numbers using a while loop.
Below is the program:
def print_primes(n):
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
i = 2
while i <= n:
if is_prime(i):
print(i)
i += 1
# Let's use the function to print prime numbers up to 30
print_primes(30)
This program uses a while loop to iterate over the numbers from 2 to the input number. For each number, it checks whether the number is prime using the is_prime function. This function uses a for loop to check whether the number has any divisors other than 1 and itself. If it doesn’t, it is a prime number and the program prints it.
The output for print_primes(30)
will be:
These are the prime numbers less than or equal to 30. In the same way, you can also print prime numbers from 1 to 20 in python.
Print the first 10 prime numbers in Python using a while loop
Here is a simple program to print the first 10 prime numbers in Python using a while loop.
Code:
def print_first_n_primes(n):
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
count = 0
i = 2
while count < n:
if is_prime(i):
print(i)
count += 1
i += 1
# Let's use the function to print first 10 prime numbers
print_first_n_primes(10)
This program uses a while loop to iterate over the natural numbers starting from 2. For each number, it checks whether the number is prime using the is_prime
function. If the number is prime, it prints the number and increments a counter count
. The loop continues until it has printed the required number of prime numbers.
The output for print_first_n_primes(10)
will be:
2
3
5
7
11
13
17
19
23
29
Print first n prime numbers in Python using a while loop
Now, let us see, another example to print first n prime numbers in Python using a while loop. The below Python program uses a while loop to print the first n
prime numbers:
def print_first_n_primes(n):
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
count = 0
i = 2
while count < n:
if is_prime(i):
print(i)
count += 1
i += 1
# To use the function, replace "num" with the number of primes you want to print
print_first_n_primes(num)
In this program, print_first_n_primes(n)
will print the first n
prime numbers. You can replace “num” with any positive integer to print that many prime numbers.
The is_prime(num)
function checks if a number num
is prime or not. If num
is prime, it returns True
; otherwise, it returns False
.
In the print_first_n_primes(n)
function, we initialize count
as 0 and i
as 2 (as the smallest prime number is 2). Then, we keep checking if i
is a prime number. If i
is prime, we print it and increment count
by 1. This process continues until count
becomes equal to n
.
Print prime numbers from 1 to 100 in Python using a while loop
Now, let us write a simple program to print prime numbers from 1 to 100 in Python using a while loop.
def print_primes(n):
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
i = 2
while i <= n:
if is_prime(i):
print(i)
i += 1
# Use the function to print prime numbers up to 100
print_primes(100)
This program uses a while loop to iterate over the numbers from 2 to the input number (in this case, 100). For each number, it checks whether the number is prime using the is_prime
function. If the number is prime, the program prints it.
Write a Python program to print prime numbers less than 20
Here is a simple Python program that uses a while loop to print all prime numbers less than 20.
Code:
def print_primes(n):
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
i = 2
while i < n:
if is_prime(i):
print(i)
i += 1
# Use the function to print prime numbers less than 20
print_primes(20)
This program uses a while loop to iterate over the numbers from 2 to the input number (in this case, less than 20). For each number, it checks whether the number is prime using the is_prime
function. If the number is prime, the program prints it.
The output for print_primes(20)
will be all prime numbers less than 20 like below:
This is how to write a Python program to print prime numbers less than 20.
Print prime numbers from 1 to n in Python
Here is a Python program that uses a while loop to print all prime numbers from 1 to n.
Code:
def print_primes(n):
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
i = 2
while i <= n:
if is_prime(i):
print(i)
i += 1
# To use the function, replace "num" with the maximum number you want to check
print_primes(num)
In this program, print_primes(n)
will print all prime numbers from 1 to n. You can replace “num” with any positive integer to print all prime numbers up to that number.
The is_prime(num)
function checks if a number num
is prime or not. If num
is prime, it returns True
; otherwise, it returns False
.
In the print_primes(n)
function, we initialize i
as 2 (as the smallest prime number is 2). Then, we keep checking if i
is a prime number. If i
is prime, we print it. This process continues until i
is greater than n
.
Python program to print prime numbers using for loop
Here is a simple Python program to print prime numbers using for loop.
Code:
def print_primes(n):
for num in range(2, n+1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
# Use the function to print prime numbers up to 30
print_primes(30)
This program defines a function print_primes(n)
that prints all prime numbers up to n
. The function uses a for loop to iterate over the numbers from 2 to n
. For each number, it checks whether the number is prime. If the number has no divisors other than 1 and itself, it is a prime number and the program prints it.
The output for print_primes(30)
will be like below:
Read: Python Program to Check Prime Numbers [6 methods]
Conclusion
In this Python tutorial, I have shown you, various examples of how to write a Python program to print prime numbers. We have covered the below queries with examples:
- Python program to print prime numbers
- print the first 10 prime numbers in Python using the while loop
- write a Python program to print prime numbers
- python program to print prime numbers using a while loop
- print first n prime numbers in Python using while loop
- print prime numbers from 1 to 20 in Python
- print prime numbers from 1 to 100 in Python using a while loop
- print prime numbers from 1 to n in Python
- write a Python program to print prime numbers less than 20
- Python find prime numbers in a range
- First n prime numbers Python
You may also like:
- Python program to find area of a triangle
- Python Program to Convert Two Lists Into a Dictionary
- Python program to sort list of tuples
- Python program for a diamond pattern
- Python program for bubble sort
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.