# Thread: Converting decimals to individuals

1. ## Converting decimals to individuals

long integer number provided by the user and displays it to the filestream stream as an integer. This requires separating the number into individual characters, converting each character into its digit value, and then displaying the resul

I'm trying to write a decout function that seperates the given long number to individual characters and the converting each character into its digit value and displaying the result. I got the algorithm done. here's what I have
Code:
```void decout (unsigned long number, FILE * stream)
{
int last_digit, second_digit, first_digit, exit_loop, COUNT;
long digit_count = 0;

while(digit_count == COUNT)
{
last_digit = number % 10 + '0'; //last digit
last_digit = number / 10;
second_digit = last_digit % 10 +'0' //second digit
first_digit = last_digit / 10;
first_digit % 10 = exit_loop;
exit_loop = COUNT;
}

}```
digits, is declared as const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; by the way.

I know there is a flaw to the algorithm but I feel like it's just a constant repetition over and over. How will I just summarize the algorithm so it works for all cases and not just three digits as my algorithm does?

Thank you so much!

2. Since you have a loop, use it to generate one character at a time until finished.

3. something of this sense?

Code:
```    while(digit_count == COUNT)
{
digit = number % 10 + '0';
number = number / 10 + '0';
number = exit_loop;
exit_loop = COUNT;
}```

4. Except presumably you should do something with digit other than just compute it. Like, say, print it to the file.

5. i think i still slightly have a trouble understanding the proper algorithm. is there a pseudo-code I can see? I'm fairly proficient in Java but new to C and I might overlooking a c method..say fputc?

Code:
```    while(digit_count == COUNT)
{
digit = number % 10 + '0';
fputc(stream,digit);
number = number / 10 + '0';
number = exit_loop;
exit_loop = COUNT;
}```

6. What you have is mostly fine. Your exit condition is not quite right (your exit condition is when you're out of digits, i.e. when your number is zero). Also you may have noticed that you are printing out your number backwards.

7. Code:
```    while(digit_count == 0)
{
digit = number % 10 + '0';
fputc(stream,digit);
number = number / 10 + '0';
number = exit_loop;
if(exit_loop == 0)
digit_count = 0;
}```
oh that was going to my question. My idea is to store them in an array and I have several ways to implement this..but adding them backwards and reading them from the back seems error-prone. But any idea if there is an recursive way to do it? Or a way to improve my array method?

8. Recursion is a fairly obvious way to do it. digit_count is not your number.

9. ohh how will I do it recursively? nothing seems to pop-up. I don't think I should use it since i'm not comfortable with it? How about using arrays? store them and read them backwards?

Code:
```    while(number == exit_loop)
{
digit = number % 10 + '0';
fputc(stream,digit);
number = number / 10 + '0';
number = exit_loop;
if(exit_loop == 0)
break;
}```

10. If you want to use an array, then yes you will have to store it and read it backwards (and also in that case you will need your digit_count so you know where to start printing from).

11. something of this sort?

Code:
```    int digit
long digit_count = 0;
int array[] = 50;

while(number == exit_loop)
{
digit = number % 10 + '0';
fputc(stream,digit);
array[50] = digit;
number = number / 10 + '0';
array[49--] = number
number = exit_loop;
if(exit_loop == 0)
break;
}```

12. Code:
```    int digit
long digit_count = 0;
int array[] = 50;

while(number == exit_loop)
{
digit = number % 10 + '0';
fputc(stream,digit);
array[50] = digit;
number = number / 10 + '0';
array[49--] = number
number = exit_loop;
if(exit_loop == 0)
break;
}```
is this the correct exit method?