Problem Statement
We have two trains:
- Train 1 stops at every 3rd station
- Train 2 stops at every 5th station
We need to find:
- First station where both trains meet
- All stations where both trains meet
- Total count of common stations
- Last common station
We will check stations from 1 to 300.
1. First Station Where Both Trains Meet
station_no = 1
while station_no <= 300:
if station_no % 3 == 0 and station_no % 5 == 0:
print("First meeting station:", station_no)
break
station_no = station_no + 1
Output
First meeting station: 15
2. All Stations Where Both Trains Meet
station_no = 1
while station_no <= 300:
if station_no % 3 == 0 and station_no % 5 == 0:
print(station_no)
station_no = station_no + 1
Some outputs:
15
30
45
60
...
300
3. Count of Common Stations
station_no = 1
count = 0
while station_no <= 300:
if station_no % 3 == 0 and station_no % 5 == 0:
count = count + 1
station_no = station_no + 1
print("Total common stations:", count)
4. Last Common Station
station_no = 1
last_station = 0
while station_no <= 300:
if station_no % 3 == 0 and station_no % 5 == 0:
last_station = station_no
station_no = station_no + 1
print("Last common station:", last_station)
Top comments (0)