unsigned long n,i=0,num=0;
Make it printf("%lu\n",i); i.e add a “\n” in your printf. rest looks fine
Did you test your code? What is the output in your system? Because even with the sample input this code will give wrong answer: you are printing everything on the same line.