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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.