Its to avoid the buffer problem.Some times the buffer may contain '\n'.Because the previous scanf will not consider the '\n' which you gave after typing the input.So when the next scanf (Here for ans ) will look on the '\n' and consider that the input for ans is available , so it will process with that .To avoid that I used getchar() ,so now the getchar will get the '\n',so the scanf can get the fresh input from the user.

I hope you understand.