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 :
Code:
#include<stdio.h>
void main()
{
int n , i ;
int stk[15] , 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--]);
}
so basically, that program will seperates number into it's individual digits.
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 !