hello everybody!
I wanted to do an exercise which ask to write a function which receives in input an int and extracts every digit from it( ex: 124--> 1 2 4).
here is my code. i cannot understand why (n%k)/k outputs always 0 (which is false!)..
thanks for your help!
PS: May you suggest me other ways of dealing with digits (eg. if I 'd like to reverse a number 123 in 321 etc)? thanks again!
The output i get is:Code:#include <stdio.h> #include <math.h> void digitSeparator (int); int main() { int n; printf("Insert a number: "); scanf ("%d",&n); digitSeparator(n); } void digitSeparator (int n) { int k=1, exit=0; while (exit==0) { if ((int)(n/k)==0)exit=1; if (n%k!=0) printf("%d\n",(n%k)/k); k*=10; } }
Code:gcc -lm eserc5_22.c -o eserc5_22 f@f-laptop:~exercises$ ./eserc5_22 Insert a number: 123 0 0 0



LinkBack URL
About LinkBacks





