Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Java program to Display all prime numbers between two intervals

Python Code to Display all prime numbers in an interval

Posted on November 26, 2020

Table of Contents

  • Python Code to Display all prime numbers in an interval
    • Display prime numbers between two intervals
      • Display prime numbers between two intervals using for loops
      • Display prime numbers between two intervals using function

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes