Python program to print prime numbers [With 8 Examples]

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 and end 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 inner for loop.
  • If no factor is found, the else block of the inner for loop is executed. The else associated with the for loop only runs when the loop has finished normally (i.e., it didn’t encounter a break).
  • In this case, it prints the number because the number is a prime number.
READ:  Write a Program to Calculate Simple Interest in Python

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.

Python program to print prime numbers

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:

Simple prime number program in python

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.

READ:  How to get the first character of a string in Python? [5 Methods]

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:

write a python program to print prime numbers less than 20
write a Python program to print prime numbers less than 20

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.

READ:  How to insert item at end of Python list [4 different ways]

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:

python program to print prime numbers using for loop
Python program to print prime numbers using for loop

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: