Print Prime Numbers in Python-DecodingDevOps

Print Prime Numbers in Python-DecodingDevOps

A Prime number is a natural number which is greater than 1 and this prime number doesn’t have the positive divisors other than 1 and itself. A prime number is a positive integer which is evenly divisible by only 1 and itself. This prime number should always be greater than 1 and it should not be written as the product of two smaller natural numbers. There are an infinite set of pair of prime numbers which differ by 2 such primes are called as the twin primes. There is exactly one set of prime number triplets. 2 is the only even prime number and the rest all are odd prime numbers.

#Python Program to print Prime Numbers from 1 to 100

for Number in range (1, 101):

    count = 0

    for i in range(2, (Number//2 + 1)):

        if(Number % i == 0):

            count = count + 1

            break

    if (count == 0 and Number != 1):

        print(" %d" %Number, end = '  ')

STEP 1: In this step, we use the for loop to iterate the value between 1 and 100 within the for loop.

STEP 2: In this step, we assign the count variable to 0.

STEP 3: In this step, we use another for loop to check the condition that whether the number is divisible or not.

STEP 4: In this step, we check the condition using the if loop, if it is true count will be incremented and the break will skip that number.

STEP 5: In this step, we use another if statement to check whether the count is zero, and the given number is not equal to 1. If it is true, it will print the number because it is a Prime Number.

OUTPUT:

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *