Saturday, 17 September 2022

Python Example programs : Prime Number

 Finding a number is prime or not in python

Example1

x = int(input("enter a number: "))
i=2
while (i<x):
    if (x%i == 0):
        print(f"{x} is not a prime number")
        break
     
    i=i+1
else:
        print(f"{x} is  a prime number")

Output

enter a number: 7727
7727 is a prime number

-------------------------------------------------------------------------------------


Example 2

x = int(input("enter a  number: "))
i=2
if x <= 0:
       print(f"{x} is not a prime number")
else:
    while (i<x):
        if (x%i == 0):
          print(f"{x} is not a prime number")
          break
     
        i=i+1

    else:
        print(f"{x} is  a prime number")

Output
enter a positive integer number: 0
0 is not a prime number

No comments:

Post a Comment