List of Primes Python Program

By | July 24, 2019

List of Primes Python Program – This Python programming tutorial will explain the Python code to show the list of all prime numbers between to given numbers. For example the list of prime numbers between 2 annd 10 is: 2, 3, 5, 7.

We will write a Python function show_all_prime(num1,num2) that will display the list of all prime numbers between two given positive numbers.

Python list of prime numbers

Python list of prime numbers

The source code of List of Primes Python Program

# Write a Python function show_all_prime(num1,num2)
# that takes a starting and ending number
# and displays all prime numbers between them.
# Use this function in a Python program.
# Author: www.EasyCodeBook.com (c)
from math import sqrt
def show_all_prime(num1,num2):
    """ This function returns true if given number is prime """
    print('List of prime numbers between',num1,'and',num2,'is:')
    for n in range(num1,num2+1):
        
        max_divisor = sqrt(n)
        max_divisor = int(max_divisor)
        flag=True
        for i in range(2,max_divisor+1):
            if n % i == 0:
               flag=False
               break
        if flag==True and n>1:
            print(n,',', end='')    


n1= int(input('Enter first number (greater than 1):'))
n2= int(input('Enter second number:'))
# call the function 
show_all_prime(n1,n2)

The output of the List of Primes Python Program

Enter first number (greater than 1):2
Enter second number:100
List of prime numbers between 2 and 100 is:
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 ,

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *