Solution 40 seconds
s= d/t
let s1 be the speed of 1st train  = d/4
let s2 be the speed of 2nd train = d/5

relative speed in same direction s= s1 - s2
                                                 = ( d/4 - d/5 )
                                                 = d/20

now t = d/s ( time taken to cross each other moving in same direction  so d= 2d because 2 trains of same length d )

    t =   \frac{2d}{ \frac{d}{20} }
      =  \frac{2d X 20}{d}     
       =40 sec

Thus the time taken to cross would be 40 seconds
2 3 2
Hi,,relative speed = (d/4 + d/5) = (9d/20). So answer will be 40/9 sec. because trains are in the opposite direction. But your method helped me a lot. Thank you
The time taken for the trains to cross each other is opposite direction must be 40/9 seconds