Write a Python Program to Print Prime Numbers Less Than 20

In this article you will learn a Python program to print the prime numbers less than 20.  As we know the prime numbers start from 2, given the range between 2 to 20, You have to write a Python logic that prints the prime numbers between 2 to 20.

Let's try to solve this using both the loops,  while loop and for loop.

 

 

Write a Python Program to Print Prime Numbers Less Than 20

 


Write a Python Program to Print Prime Numbers Less Than 20

So we will be dividing the given number from 2 until the square root of the number. Let's import the square root function from the Math module.

Take a variable named as numerator and initialize it to 2.  The numerator variable will iterate from 2 - 20 using a while loop. Also print a user friendly message using the print function call as shown below.

from math import sqrt

numerator = 2
print("The prime numbers less than 20 are ...")




Now use the while loop until the numerator is less than 20,  calculate the limit by finding the square root of the numerator and adding 1 to it. Use the for loop to get the range from to the  square root limit.  use the if statement to check if numerator is divisible by the denominator if so break the for loop.

Use the for loop else block to  print the numerator if all the iterations of the following are done,  increment in the numerator.

while numerator < 20:
   limit = int(sqrt(numerator)) + 1
   for denominator in range(2, limit):
       if numerator % denominator == 0:
           break
   else:
       print(numerator, end=" ")
   numerator += 1



The above logic will print the prime numbers that are less than 20.




Complete Python Program
==========================

from math import sqrt

numerator = 2
print("The prime numbers less than 20 are ...")

while numerator < 20:
   limit = int(sqrt(numerator)) + 1
   for denominator in range(2, limit):
       if numerator % denominator == 0:
           break
   else:
       print(numerator, end=" ")
   numerator += 1





Output
================

The prime numbers less than 20 are ...
2 3 5 7 11 13 17 19




Python Program Using For Loop
=====================================

from math import sqrt

print("The prime numbers less than 20 are ...")

for numerator in range(2, 20):
   limit = int(sqrt(numerator)) + 1
   for denominator in range(2, limit):
       if numerator % denominator == 0:
           break
   else:
       print(numerator, end=" ")





Conclusion
===============

The above Python program uses the while loop to get the numerator range between 2 to 20.  Also we need another range of denominators  that is from to square root of the numerator.

Once we get the numerator and denominator we try to perform the modulus operation to check if the remainder is zero then break the for loop.  If all the iterations of the denominator are completed then print the numerator as prime number.


Execute the above Python program by modifying the number 20 that is constant, If the Python program changes from 20 to 50/100 modify it appropriately.




Post a Comment

0 Comments