# Thread: Converting decimals to individuals

1. i understand in Java that code would be trivial.. can you give me an example in C, how I would implement that not directly (since it just gives it away) but something similar? thank you!

2. Your description of what you're trying to do is fairly baffling. Have you been reading up on Rube Goldberg, perhaps?

If you want to pick out each digit from the integer, you can do that, by using the characteristics of the base 10 numbering system:

Roughly:

Code:
```while(yourInt > 0)  {
digit1 = yourInt  % 10
print digit1
yourInt /= 10
}```
If you want something else, give me an example, of the typical input, and desired output.

3. For which, the recursion? You're pretty much there:
Code:
```if (number < 9)
print number
else
digit = number %10;
same_function(number/10);
print number```

4. Code:
```void decout (unsigned long number, FILE * stream)
{
int converted[64];
long digit_count = 0;
int base = 10;
int index = 0;

while(digit_count != 0)
{
converted[index] = number % base;
number = number / base;
index++;
}

--index;  /* back up to last entry in the array */
for( ; index>=0; index--) /* go backward through array */
{
printf("%c", digits[converted[index]]);
}
}```

5. how is this?

6. That seems reasonable.

7. but i feel like 64 is a magical number..but that's the only way I got it to work.. what does it represent?

8. 64 is the length of your array, duh. Any number works that is at least 10, since (usually) an unsigned long cannot exceed 4billion.

9. so i can just replace 64 with base right? cause I also have a hexout function.

10. Well, no. You are replacing it with 10 because 4billion requires ten decimal digits. That same 4billion requires a mere eight hexadecimal digits.

11. Code:
```void decout (unsigned long number, FILE * stream)
{
int converted[64];
long digit_count = 0;
int base = 10;
int index = 0;

while(digit_count != 0)
{
converted[index] = number % base;
number = number / base;
index++;
}

--index;  /* back up to last entry in the array */
for( ; index>=0; index--) /* go backward through array */
{
printf("%c", digits[converted[index]]);
}
}```
I have a question here..how would I do this without using printf in the for-loop?

thanks!

12. Using putc? Isn't that where you started?

13. Code:
```    //base-conversion algorithm
while(number != 0)
{
converted[index] = number % base;
number = number / base;
index++;
}
// back up to last entry
--index;
// go backward through array
for( ; index>=0; index--)
{
fputc(digits[converted[index]],stream);
}```
is this reasonable?