Only even numbers are getting printed. :(

Your approach is not correct. Some cases are also giving segmentation fault.
I would suggest you watch this video.

After that see the following code that I have written: