# Thread: Separating a Number into its Digits

1. ## Separating a Number into its Digits

How do you separate an integer in its individual digits without using an array? I'm thinking I have to divide it somehow...

I need to take the individual digits from an integer and convert them into their respective english names. I have seen answers with arrays, but I have not yet learned about arrays in the book that I'm using.

2. So, if you have an integer 125, you want to print "one two five"?

I guess the array solution is to turn it into a string and go through each character.

Without arrays... indeed, you can get each digit out using division. To print from left to right you'll have to pick an upper limit. Say you decided your limit is 4 digits (it should be higher ), you'd first divide by 10^3, then 10^2, 10^1, 10^0 to get each digit. You can easily do this in a loop. Just don't forget that in C, raising something to a power is done with pow(), not with "^" as you might expect.

Pseudo code ish:

Code:
```	<i is the number input>

index = 6
while index >= 0
partial = i/(10.0^index)
if (partial != 0)
print word for partial
i = i - partial * (10.0^index)

index = index - 1;```
Yuck. Wonder if someone else can do better

3. I thought it might help if I posted the code that I have so far:

// Program to take integers entered and display each digit in english.

Code:
```#include <stdio.h>

int main (void)
{
int integer = 0, output = 0;

printf ("Enter an integer: ");
scanf ("%i", &integer);

if ( integer > 99999999 )
printf ("Integer is too large!");

switch (output = integer % 10)
{
case 0:
printf (" Zero ");
break;
case 1:
printf (" One ");
break;
case 2:
printf (" Two ");
break;
case 3:
printf (" Three ");
break;
case 4:
printf (" Four ");
break;
case 5:
printf (" Five ");
break;
case 6:
printf (" Six ");
break;
case 7:
printf (" Seven ");
break;
case 8:
printf (" Eight ");
break;
case 9:
printf (" Nine ");
break;
}

return 0;
}```

4. There's an easier way to peel off the digits of a number. This works by "peeling" off the 1's digit, with every iteration through the loop. In pseudo code

Code:
```while(number > 0)
variable = the number % 10
divide the number by 10
logic to print the variable's word, in here
end while loop```

There's an easier way to peel off the digits of a number. This works by "peeling" off the 1's digit, with every iteration through the loop. In pseudo code

Code:
```while(number > 0)
variable = the number % 10
divide the number by 10
logic to print the variable's word, in here
end while loop```
This separated the digits but also reversed the so 567 = Seven Six Five

Inserted it like this:

Code:
```    if ( integer > 99999999 )
printf ("Integer is too large!");

while ( integer > 0 )
{
output = integer % 10;
integer = integer / 10;
if ( output != 0 );
switch ( output )```

6. If you want the digits in the correct order then an easy way is to start with the largest power of 10 (i.e. 1 billion) and then work your way down towards 1. Each time through the loop divide by that power of 10.
One you get your first non-zero result then you start outputting text.
Also, subtract the required amount to remove that digit as you go.

7. Originally Posted by iMalc
If you want the digits in the correct order then an easy way is to start with the largest power of 10 (i.e. 1 billion) and then work your way down towards 1. Each time through the loop divide by that power of 10.
One you get your first non-zero result then you start outputting text.
Also, subtract the required amount to remove that digit as you go.
What would that code look like?

8. Originally Posted by iMalc
If you want the digits in the correct order then an easy way is to start with the largest power of 10 (i.e. 1 billion) and then work your way down towards 1.
That hard codes an upper limit on what values can be handled. It certainly won't work for long long types.

This strikes me as a bit of a homework problem. However, the solution involves using some means of temporary representation for data of varying size, presumably distinct from an array. There is an anomaly in the question in that the words "one", "two", etc are also technically represented as arrays.

9. Originally Posted by grumpy
That hard codes an upper limit on what values can be handled. It certainly won't work for long long types.

This strikes me as a bit of a homework problem. However, the solution involves using some means of temporary representation for data of varying size, presumably distinct from an array. There is an anomaly in the question in that the words "one", "two", etc are also technically represented as arrays.
The book does say that its a difficult exercise. I'm not taking a class, just learning on my own from a book and some free lectures online.

10. It's not that difficult.

An array is just a data structure that supports random access to elements that have a defined order. There are other data structures you can use in order to retrieve elements in reverse order, relative to how they are stored.

11. Originally Posted by grumpy
That hard codes an upper limit on what values can be handled. It certainly won't work for long long types.
Yes indeed it means limiting the maximum number, and in fact if this were used on a compiler that had 16-bit ints then it would require a different maximum value. But if one wants to avoid using an array, then short of using recursion instead its probably the best option.
Portability is naturally less of a concern for a beginner.

How about using a lookup table for the number names.

12. Or you could take the number as a string and walk it. Or you could take the number as some integer type, and use recursion.

Quzah.

13. Hi, Peteski, try this code that separates a 5-digit integer into its digits:

Code:
```#include <stdio.h>

int main(void)
{
int d1, d2, d3, d4, d5, number;

printf("Enter a five-digit number: ");
scanf("%d", &number);

d5 = number % 10;
d4 = (number / 10) % 10;
d3 = ((number / 10) / 10) % 10;
d2 = (((number / 10) / 10) / 10) % 10;
d1 = ((((number / 10) / 10) / 10) / 10) % 10;

printf("The first digit is  %d.\n", d1);
printf("The second digit is %d.\n", d2);
printf("The third digit is  %d.\n", d3);
printf("The fourth digit is %d.\n", d4);
printf("The fifth digit is  %d.\n", d5);

return 0;
}```

14. You don't need to store each of the digits in an int, as their range is from zero to nine. A simple char would work.

Also, replace the calculations with the following, as it does less operations and is more efficient.

Code:
```    d5 = number % 10;
d4 = (number / 10) % 10;
d3 = (number / 100) % 10;
d2 = (number / 1000) % 10;
d1 = (number / 10000) % 10;```