×

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

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Standard Scaler in SKLearn

The sci kit learns in python is a library thatch is used in machine learning which is used to work on data modeling.It is only focused on the data modeling,...

4 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes read.

Python md5() function

Python md5() function The md5() function in PHP calculates the md5 hash of a string. Syntax md5 ( string $str [, bool $raw_output] ) Parameter str(required)- This parameter specifies the string. raw_output(optional)- This parameter specifies a hex or binary output format: TRUE - Raw 16 character binary...

1 minute read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python String replace() method

Python String replace() method The string.replace() method in Python returns a copy of the string with all occurrences of substring old replaced by new. Syntax replace(old, new[, count]) Parameter old: This parameter represents a substring you want to...

2 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Data Types

Python Data Types: The variable in Python is used to store values, and they can hold different values. Each variable in Python has its own data-type. Since Python is a...

13 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python GUI Programming

GUI (Graphical User Interface) GUI is a graphics-based operating system that uses icons and menus to interact with the user. Python mostly works on CLI (Command Line Interface). Widgets Any user interface has...

4 minutes read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Yield vs Return in Python

In this article, you will learn about " Yield " and " Return " in Python. You will also understand the difference between " Yield " and " Return "...

6 minutes read.