Python - Display All the Prime Numbers

A prime number is a whole number greater than 1 with only two factors - itself and 1.
A prime number can not be divided by another positive integer without leaving a remainder, decimal or fraction.
2,3,5,7,11 etc are the prime numbers as they do not have any other factors.

In this program, you will learn how to find the prime numbers within an interval.


Program Code



    # To display all the prime numbers within an interval

    #num = 127
    lower = int(input('Enter Lower Number: '))
    upper = int(input('Enter Upper Number: '))

    for num in range(lower, upper + 1):
        if num > 1:
            for i in range(2, num):
                if(num % i) == 0:
                    break
            else:
                print(num)

Output



    Enter Lower Number: 100
    Enter Upper Number: 200
    101
    103
    107
    109
    113
    127
    131
    137
    139
    149
    151
    157
    163
    167
    173
    179
    181
    191
    193
    197
    199

In this program, we are using lower and upper to hold the lower internval and upper interval values respectively using range function.



The following topics can be useful to understand this program.



Related Tutorials