i'm still very new at this, and i found this code that could help me on my project. but i can't quite get the logic behind it. the code is like this :
so basically, that program will seperates number into it's individual digits.
int n , i ;
int stk , top = 0;
printf("Enter the number :");
scanf("%d" , &n);
while( n > 0 )
i = n % 10;
stk[++top] = i;
n = n / 10;
while(top)printf("%d " , stk[top--]);
the stuff that i don't quite get is this :
- why using a loop ? can it be solve by logical if ?
- why the code is not working if i 'change stk[++top] = i' into 'stk[top++] = i'
- what is the meaning of "while(top)printf("%d " , stk[top--])" ? i mean why use 'while' ? and why you use 'top--' ?
that is all. thx !