In this story one thief and one police , there is 40feet distance between them, thief try to escape from police , but he to cover only 2 feet in a step, but police cover 5 feet in a single step, police how many feets needs to catch thief ?
theif = 40
police = 0
while theif > police:
theif += 2
police += 5
print(theif)
OUTPUT;
68
First we create variable for thief and police , police is 0 and thief is 40 , because 40 is the distances of them , then we create while loop when police greater than thief , this while loop is stop, inside the while loop increase by 2 and police increase by 5 feet after while loop end , i was printed thief.
now we understand police needs 68 feet to catch thief
In this story there are two trains , train1 is stop every third station , and train2 is stop every 5th station , Question is which station these two trains will meet.
train = 0
while train < 30:
train = train + 1
if train % 3 == 0 and train % 5 == 0:
print("train will meet in ", train)
OUTPUT:
train will meet in 15
train will meet in 30
First i created one variable commonly train and it's value is 0 , i set while loop condition with in 30 that's mean until 29 this while will run , then inside while loop have train = train + 1,
and if condition , what the if condition is if train count is divisible by 3 and 5 then this condition will true , then execute what inside the condition , now print
train will meet in 15
train will meet in 30
we have confusion in 30 , like how it run because while loop condition is < 30 , after that in inside of while loop 29 +1 , then only we did if condition so it will print (30).
Top comments (0)