Prime Number in an Interval Using Python

prime number interval program in python
Reading Time: 5 minutes

Python is a popular and versatile programming language that can be used for various applications, such as web development, data analysis, machine learning, and more. One of the advantages of Python is its simplicity and readability, which makes it easy to write and understand code.

One of the applications of Python is to find prime numbers in an interval. Prime numbers are natural numbers that have exactly two positive divisors: 1 and themselves. For example, 2, 3, 5, 7, 11, etc. are prime numbers. Finding prime numbers is useful for cryptography, number theory, and other mathematical fields.

To find prime numbers in an interval using Python, we can use a simple algorithm that checks each number in the interval for divisibility by any number from 2 to its square root. If the number is divisible by any such number, it is not prime. Otherwise, it is prime. We can use a for loop to iterate over the interval and a function to check for primality. Here is an example of a Python program that finds prime numbers in the interval [10, 30]:

Algorithm Explanation:

  1. Import the math module, which provides mathematical functions.
  2. Define a function is_prime(num) to check if a number is prime or not.
  3. Inside the is_prime() function, check if the number num is less than or equal to 1. If so, return False since prime numbers are greater than 1.
  4. Iterate from 2 to the square root of num (inclusive) using a for loop and range. Check if num is divisible by any number in this range. If it is, return False since it is not prime.
  5. If no factors are found, return True, indicating that the number is prime.
  6. Define another function print_prime_numbers(start, end) to print all prime numbers in the given interval.
  7. Initialize an empty list prime_numbers to store the prime numbers found.
  8. Iterate through all numbers in the given interval from start to end (inclusive) using a for
  9. For each number, call the is_prime() function to check if it is prime. If it is, add it to the prime_numbers
  10. Return the prime_numbers
  11. Take user input for the starting and ending numbers of the interval.
  12. Call the print_prime_numbers() function with the given interval and store the prime numbers in the prime_nums
  13. Finally, print the prime numbers one by one using a for prime number

To determine if a Prime Number program in Python, you can use the following code:


     import math
def is_prime(num):
    """Function to check if a number is prime or not."""
    if num <= 1:
        return False
    for i in range(2, int(math.sqrt(num)) + 1):
        if num % i == 0:
            return False
    return True
def print_prime_numbers(start, end):
    """Function to print all prime numbers in the given interval."""
    prime_numbers = []
    for num in range(start, end + 1):
        if is_prime(num):
    return prime_numbers
# Input the interval from the user
start = int(input("Enter the starting number of the interval: "))
end = int(input("Enter the ending number of the interval: "))
# Print the prime numbers in the given interval
print(f"\nPrime numbers between {start} and {end} are:")
prime_nums = print_prime_numbers(start, end)
for prime_num in prime_nums:



     Enter the sample number of the interval : 10
Enter the ending number of the interval : 30



     Prime number between : 10 and 30 are