Python Examples For Beginners| Check Prime Number

Python Examples For Beginners — Check Prime Number

Python Examples For Beginners| Check Prime Number

# take user input a = input("Enter the number: ") 
a = int(a) //by-default user inputs are str so to calculate further we need to cast it to int
x = 0
if (a > 3):
for i in range (2, a):
if(a % i) == 0:
x = 1;
break
else:
x = 0
if(x == 1):
print(a, "is not a prime number")
else:
print(a, "is a prime number")

Explanations:

Take an user input

# take user input 
a = input("Enter the number: ")
a = int(a) //by-default user inputs are str so to calculate further we need to cast it to int

Initialization and condition

Start the loop

for i in range(1, a):
for i in range(2, a):
if(a % i) == 0: 
x = 1;

break
if(a % i) == 0: 
print(a, " is not a prime number")
else:
print(a, " is a prime number")

Output

Please enter the number: 7 
7 is a prime number

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
CodingHub

CodingHub

Coding Hub — Learn & Build Your Career