DEV Community

Cover image for PYTHON SOLVED PROBLEMS
Vinoth Kumar
Vinoth Kumar

Posted on Edited on

PYTHON SOLVED PROBLEMS

Q1) Two trains stop at regular intervals:
Train 1 stops at every 3rd station.
Train 2 stops at every 5th station.
Find:
The first common station where both trains stop.
The last common station where both trains stop.
All common stations where both trains stop.

Note: Total Number of Stations are unknown.

SOLUTION :

station = 1
last_station = station
while station <= 15:
    if station % 3 == 0 and station % 5 == 0:
        print(station)
        print('First common station: ', station)
        break
    station+=1
Enter fullscreen mode Exit fullscreen mode

OUTPUT :

15
First common station:  15
Enter fullscreen mode Exit fullscreen mode

SOLUTION 2:

let station=15;
let last_station = station;
while (station <=15){
if(station % 3 == 0 & station % 5 ==0 ){
console.log(station);
}
last_station =station;
console.log('first common station: ',station)
station+=1;
}




**OUTPUT :**

![ ](https://dev-to-uploads.s3.us-east-2.amazonaws.com/uploads/articles/m4jr79sevqfq37h6t11z.png)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)