How to Print all Prime Numbers in an Interval using Python?



Prime number is defined as a number not divisible by any other number except 1 and itself. Hence to ascertain that a number is prime, it should be checked for divisibility by all numbers between 1 and itself excluding both.

Following program lists all prime numbers between 1 to 100. Outer loop generates numbers in this range. Inner loop goes from 2 to each number in outer loop and successively checks divisibility by % operator. If it is not divisible by all numbers in inner range, it prints out that number

for i in range(101):
    for j in range(2,i-1):
        if i%j==0: break
    else:
        print (i)

Advertisements