1.train1 is stop every 3rd station and train2 is stop every fifth station , we should print train 1 ,every 3rd station and train 2 every 5th station , if two trains meet same station then we print both ?
train = 0
while train < 30:
train = train + 1
if train % 3 == 0 and train % 5 == 0:
print("both")
elif train % 3 == 0:
print("Train1")
elif train % 5 == 0:
print("Train2")
output:
Here train is count of station , and while loop condition is train less than 30 , then train will increase by one in inside the while loop,
then we do if and elif , first if, if train divisible by 3 and 5 then i print "both", elif train divisible by 3 then we will print train1 and elif train divisible by 5 then train2 what we print.
Here we should put the (and) condition first why because if we put 3 or 5 of instead of both , then what will happen is incase if condition get true then elif not working. we also want to both divisible of 3 and 5 right.in case 3 and 5 also get divisible at that time if we put 3 divisible in if condition incase that will true then elif condition will not work , so we missed the both train meeting station.
2.Train1 will stop everry 3rd station , train 2 will stop every 8th station , we should print the station no and station count and which is last station number?
train = 0
count = 0
while train < 300:
train = train + 1
if train % 3 == 0 and train % 8 == 0:
count+=1
print("station no :",train)
print("totalstation is ",count)
Output:
same concept like except some condition,
here we create count variable to print total count station where is two train will meet, and we print all stations numbers.
we have to duscuss about how to print last station .


Top comments (0)