Write Python Function to Print All Prime Numbers Between 1 to 100

A prime number is a number that is completely divisible by 1 and the number itself.  it doesn't have any other factors that can divide the prime number completely.

For example let's take 11,  11 has only two factors that is 1 and 11 itself none of the number can divide the 11 completely

Let's take another example 22,  2 number 22 has factors such as 1, 2, 11, and 22.  so it has four factors so 22 is not a prime number.
 
 
 
Write Python Function to Print All Prime Numbers Between 1 to 100

 

Write Python Function to Print All Prime Numbers Between 1 to 100

Let's define the function getprime100().  using the for loop iterate from 2 to 100 and get (numerator) x value. Using another for loop, iterate from the 2 to x and get the y value (denominator) as shown below.

Divide the numerator with the denominator X divide by Y and check if the reminder is zero if so break the for loop.  using the else part of the for loop print the prime number using the print function call.

def getprime100():
    for x in range(2, 100+1):
        for y in range(2, x):
            if x % y == 0:
                break
        else:
            print(f"The Prime Number is : ", x)




In the main program print the  the user friendly message that is prime numbers from 1 to 100 are,  and call the  the above function function named as getprime100()

print("Prime Number between 1 - 100 is ...")
getprime100()
 
 
 
 
 
Complete Program
==============================
def getprime100():
    for x in range(2, 100+1):
        for y in range(2, x):
            if x % y == 0:
                break
        else:
            print(f"The Prime Number is : ", x)


print("Prime Number between 1 - 100 is ...")
getprime100()





OUTPUT
=================
Prime Number between 1 - 100 is ...
The Prime Number is :  2
The Prime Number is :  3
The Prime Number is :  5
The Prime Number is :  7
The Prime Number is :  11
The Prime Number is :  13
The Prime Number is :  17
The Prime Number is :  19
The Prime Number is :  23
The Prime Number is :  29
The Prime Number is :  31
The Prime Number is :  37
The Prime Number is :  41
The Prime Number is :  43
The Prime Number is :  47
The Prime Number is :  53
The Prime Number is :  59
The Prime Number is :  61
The Prime Number is :  67
The Prime Number is :  71
The Prime Number is :  73
The Prime Number is :  79
The Prime Number is :  83
The Prime Number is :  89
The Prime Number is :  97





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

With the help of above function try to modify it  to print Prime numbers from 1 to 1000,  comment down below if you have any suggestions  or queries regarding the above program





Post a Comment

0 Comments