×

Python Program to Print all the Prime Number in an Interval

Python Program to Print all the Prime Number in an Interval

What is Prime numbers?

A prime number is referred to those numbers that can be divisible by them only. In simple words, those numbers can't divisible by a number lower than them. For example, if we take 13 as input then we check that the 13 is divisible by 2 or not, if not then we check if the number is divisible by 3 or not, we check this till 12, if till 12 no one can't be able to divide 13 completely so 13 is a prime number. If the 13 is divided completely by the number lower then it is not a prime number.

In the language of programming language, we will use a loop till 13 and check that 13 is divisible by the lower value or not. If it is divided by the lower value then we stop the execution and print that it is not a prime number else it is a prime number.

Numerical Example

Take a number=5 

Now, we have to check the prime number then we simply start dividing 5 with 2 not with 1 because anything divided by 1 its remainder is 0.

Now, after dividing 5 by 2 we get 2 in dividend and 1 in the remainder,

Now, divide 5 by 3 we get 1 in dividend and 2 in the remainder,

Now, divide 5 by 4 we get 1 in dividend and 1 in the remainder.

Hence, 5 is not completely divisible till 5, so 5 is a prime number.

How to check the prime numbers in python?

Let’s Understand an Example with the help of the program.

 #taking an input from the user
 num = int(input("Enter the number to check: "))
 #Declaring a variable 
 #which is required for the program
 flag=0
 #executing the For loop
 for no in range(2,num):
     #Checking for prime number
     if (num%no)==0:
         #Required for further checking
         flag=1
         #stop the execution of loop
         break
 #Checking the flag value 
 # for final declaration of prime number
 if flag==1:
     print(num," is not a prime number")
 else:
     print(num," is a prime number") 

Output:

 Enter the number to check: 13
 13 is a prime number
 Enter the number to check: 10
 10 is not a prime number 

Explaination:

In the above example, Firstly we ask the user to input a number and convert it from string to number with the help of the int() function and store it in a variable named num. Then we declare a variable flag that will help in the further procedure. Now, we used a for loop to implement the "no" variable in the range of 2 to the user entered number.

then we check the remainder of the num divided by no is equal to 0 or not with the help of if control statement. if the remainder is 0 then initialize the flag by 1 and stop the execution else continue. At last, if the flag value is 1 then the entered value is not a prime number else it is a prime number.

How to print all the Prime Numbers in an interval?

To print all the prime numbers existing in the interval we will implement two loops one to check the prime number and the other to check the number is in the interval or not. In this we are going to see 2 different methods to perform this program.

  • Without the Function

Program:

Let us understand this query with the help of the program

 #taking interval from the user
 lower = int(input("Enter the lower Interval value: "))
 upper = int(input("Enter the upper Interval value: "))
 prime=[]
 #loop through the Interval and check individually
 for num in range(lower, upper+1):
         #Number should not be divisible
         #by any number between 1-num
         for no in range(2,num):
             if (num%no)==0:
                 break
         else:
             prime.append(num)
 print(prime) 

Output:

 Enter the lower Interval value: 100
 Enter the upper Interval value: 150
 [101, 103, 107, 109, 113, 127, 131, 137, 139, 149]
 Enter the lower Interval value: 10
 Enter the upper Interval value: 50
 [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] 

Explaination:

In the above example, firstly we ask the user to input the lower limit and upper limit. then we created a blank list named prime. then we implemented a For loop to check that the num is in the range of lower and upper limit. Then we declare a nested for loop to check that the "no" is in the range of 2 to the num. After that, we check that the remainder of num divided by no is equal to 0 or not. if it is equal to 0 then it stops the execution of the loop. and if the no is not in the range of 2 to num then it appends that number in the prime list. After all, execution is completed then we printed the list.

  • With the Function

Program:

Let us understand this Program with the simple example.

 #creating the function
 def prime(upper,lower):
     #creating a blank list
     prime=[]
     #loop through the Interval and check individually
     for num in range(lower, upper+1):
         #Number should not be divisible
         #by any number between 1-num
         for x in range(2,num):
             if num%x==0:
                 break
         else:
             #print(num)
             prime.append(num)
     return prime
 #taking limits from the user
 lower = int(input("Enter the lower Interval value: "))
 upper = int(input("Enter the upper Interval value: "))
 #calling prime function
 prime=prime(upper,lower)
 #printing the prime list
 print(prime) 

Output:

 Enter the lower Interval value: 100
 Enter the upper Interval value: 150
 [101, 103, 107, 109, 113, 127, 131, 137, 139, 149] 

Related Topics

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

How to run a Python file in CMD

Introduction Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code...

4 minutes read.

How to slice a list in python

When working with lists, we face situations where we may need a part of the list from one index to the other. Slicing is one of the simpler ways to...

5 minutes read.

Python Combination

Python Combination Permutations and combinations are the important part of our mathematical tools. We use them often in our Python programs. In this tutorial, we will learn about combinations in Python...

6 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Palindrome In Python

What is Palindrome? A Palindrome can be defined as the number or a string that resides unchanged when it is reversed. Example: 14341 Output: Yes, this is a Palindrome number Example: RACECAR Output: Yes, this...

2 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Amazon rekognition using python

Amazon recognition service is an AI service which is an image labelling API (Application programming interface). In this article, we shall learn to use the AWS python boto3 module to...

3 minutes read.

Python Algorithms

A quote goes, "A goal without a plan is just a wish." To achieve anything in life, we can't simply go for it without making a plan and sticking to...

4 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

Gaussian elimination in python

Linear and polynomial equations are used in almost all fields of numerical simulation. However, its most common use in engineering is in the area of linear system analysis. The broader...

3 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Python maketrans() function

Introduction A static method is the string maketrans() one. It is used to make a one-to-one mapping between a string's character and its translation, i.e., to define the list of characters...

5 minutes read.