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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes