# Thread: problem with this expression: (n%k)/k

1. ## problem with this expression: (n%k)/k

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&#37;k)/k outputs always 0 (which is false!)..

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!

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;
}

}```
The output i get is:

Code:
```gcc -lm eserc5_22.c -o eserc5_22
f@f-laptop:~exercises\$ ./eserc5_22
Insert a number: 123
0
0
0``` 2. n&#37;k will always be < k, thus dividing the result by k will be < 1, and thus rounded to 0. 3. Yes, for integer values, I would expect (n%k)/k to always be zero - since for non-zero K, (n%k) is always less than k, and an integer division of a/b where b > a and b != 0, the result is zero.

--
Mats 4. http://cpwiki.sf.net/User:Elysia/Indentation
You badly need to learn to indent properly. 5. ## i cannot understand this.. My reasoning is what follows:

if i use n%k i get something like this:
Code:
```Insert a number: 123
3
23
123```
what i want is "3 2 1" and this is exactly (int)3/1, (int)23/10=2, (int)123/100=1.This sequence 1, 10, 100,.. is exactly k..
what is wrong in my mind? can you give me some hints to overcome this problem? thanks a lot! 6. Perhaps you shouldn't divide by k in the same line?

--
Mats 7. Originally Posted by matsp Perhaps you shouldn't divide by k in the same line?

--
Mats
no i tried already all these things...  8. no i tried already all these things...
What did you try along the lines of not dividing by k in the same statement? 9. Originally Posted by laserlight What did you try along the lines of not dividing by k in the same statement?
i tried also this
Code:
```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%k));
int a=(n%k); printf("%d\n",(a/k));

k*=10;
}
}```
and the output is:
Code:
```Insert a number: 346
0
6 0
46 0
346 0```  10. I think you have the rough idea, but are struggling to express it in code. Compile and run this example:
Code:
```#include <stdio.h>

int main(void)
{
int n = 346;
printf("&#37;d\n", n % 10);
n /= 10;
printf("%d\n", n % 10);
return 0;
}```
Based on what you conclude from this, correct your own code. 11. Originally Posted by laserlight I think you have the rough idea, but are struggling to express it in code. Compile and run this example:
Code:
```#include <stdio.h>

int main(void)
{
int n = 346;
printf("%d\n", n % 10);
n /= 10;
printf("%d\n", n % 10);
return 0;
}```
Based on what you conclude from this, correct your own code.
thank you very much laserlight! with your hint i wrote immediately another function "reverseDigits()" and then came back to my problem which I solved as follows:

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 digits = ceil(log10(n));
if (digits==0) digits++;

int k=pow(10,digits-1);
printf("K:%d\n",k);

while (1)
{
if (k>0)
{
if (n/k==0) break;
printf("%d ",(int)(n/k));

n=n%k;
if ((k/10)>0)k/=10;
else break;
}

}

printf("\n");

}```
thanks again! Bye It is still a little too complicated.

By the way, you need to indent your code properly. 13. I consider this piece of code
Code:
```void digitSeparator (int n)
{

int digits = ceil(log10(n));
if (digits==0) digits++;

int k=pow(10,digits-1);
printf("K:%d\n",k);

while (1)
{
if (k>0)
{
if (n/k==0) break;
printf("%d ",(int)(n/k));

n=n%k;
if ((k/10)>0)k/=10;
else break;
}

}

printf("\n");

}```
rather hard to read for what it does. Try rearranging (restructuring/indenting) it.

Also, it won't work for specific values (10^X where X > 0). In fact, it won't work for numbers whos last digits = 0. Example, 1240, 12400. Basically, your code, beside being illisible, is wrong! (Also, you should verify if n is positive at the beginning of your function.) 14. Does anyone note how newbies always tend to ignore suggestions that's outside the topic of what they're trying to do, like not using scanf("&#37;s", ...) or don't use void main or indent properly? Popular pages Recent additions 