Python

Python Code to Display all prime numbers in an interval

Python Code to Display all prime numbers in an interval

In this article, we will discuss the concept of Python Code to Display all prime numbers in an interval

In this program, we are going to learn how to write the code to display all prime numbers between two intervals using different methods in Python language.

This is done using for loop and function in Python language

Display prime numbers between two intervals

Display prime numbers between two intervals using for loops

In this program, we will print prime numbers between two intervals  using a for loop in Python language

Program 1

#Get input from the user
lower=int(input("Enter a vaue for lower: "))
upper=int(input("Enter a vaue for upper: "))
for num in range(lower,upper+1):
   if num>1:  #prime numbers are greater than 1
     for i in range(2,num):
       if(num%i==0):
         break
     else:
         print(num)

When the above code is executed, it produces the following result

Enter a value for lower: 15
Enter a value for upper: 55
17
19
23
29
31
37
41
43
47
53

 

 

Display prime numbers between two intervals using function

In this program, we will print prime numbers between two intervals  using a function in Python language

Program 2

# Display prime number in given interval
#function definition for display prime
def dis_Prime(n):
    if n<2:
        return False
    for i in range(2,n):
        if n%i==0:
            return False
    return True
    
    #Reading interval value from user
max=int(input("Enter maximum value: "))
min=int(input("Enter minimum value: "))
    
print('prime numbers from %d to %d are: '%(min,max))
for i in range(min,max+1):
        if dis_Prime(i):
            print(i)

When the above code is executed, it produces the following result

Enter maximum value: 95
Enter minimum value: 5
prime numbers from 5 to 95 are:
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89

 

Suggested post

Python function

For loop in Python

while loop in Python

Operator in Python

Data types in Python

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.