﻿ Interview Preparation

# Control flow: while loop

python while loop statement: As many people are asking questions about prime numbers program, please check this one

p = 10
q = 16
# we will keep checking every number starting from p to q
for i in range(p, q):
print("we are checking ", i, "is prime or not")
# we assume that i is not divisible by any number except 1 and i
is_i_divisble_by_any_number = False
# now, we will start searching for a number that devides i in range 2, i
for j in range(2, i):
# lets check if j devides i or not
if i % j == 0:
# since there exist a number j that devides i, our assumption is wrong
# so we are setting is_i_divisble_by_any_number = True
is_i_divisble_by_any_number = True
print(" the number ", j, "devides ", i)
# since we got the number, so we proved that its not a prime numer
# so we will break it
break
# if our assumption still holds even after the search in range(2, i)
# we declare i as prime number
if is_i_divisble_by_any_number == False:
print(i, "is a prime number")
else:
print(i, "is not a prime number")
print("="*50)


Output of the of the code

we are checking  10 is prime or not
the number  2 devides  10
10 is not a prime number
==================================================
we are checking  11 is prime or not
11 is a prime number
==================================================
we are checking  12 is prime or not
the number  2 devides  12
12 is not a prime number
==================================================
we are checking  13 is prime or not
13 is a prime number
==================================================
we are checking  14 is prime or not
the number  2 devides  14
14 is not a prime number
==================================================
we are checking  15 is prime or not
the number  3 devides  15
15 is not a prime number
==================================================