Python Program to Generate Prime Numbers in an Interval

This python program generates prime numbers in an interval given by user.

A Prime Number is a positive integer greater than 1 which is divisible by 1 and itself.

Also try: Check Prime Number Online & Generate Prime Numbers Online

Python Source Code: Generate Prime Numbers


# Generate prime numbers in interval

# Function to check prime

def is_prime(n):
    if n< 2:
        return False
    
    for i in range(2,n):
        if n%i == 0:
            return False

    return True


# Reading interval from user
min_value = int(input('Enter minimum value: '))
max_value = int(input('Enter maximum value: '))

# Looping & displaying if it is prime
# Here min_vale & max_value are included
print('Prime numbers from %d to %d are:' %(min_value, max_value))
for i in range(min_value, max_value+1):
    if is_prime(i):
        print(i, end=' ')

Prime Generation: Output

Enter minimum value: -10
Enter maximum value: 100
Prime numbers from -10 to 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 

Prime Generation: Code Explanation

We first read min_value and max_value from user. Function is_prime() is used to check whether a given number is prime or not. We loop from min_value to max_value and pass each number to is_prime() function. If this function returns True, then we print it.