Code is giving wrong output , pls help in finding the mistake