# Quick Question

Printable View

• 02-27-2006
comwiz
Quick Question
Hey all,
I have a question. I want to break up an integer by digits. For example,
Code:

```1234 1 2 3 4```
Does anybody know if this is possible to do.
Thanks,
Kyle
• 02-27-2006
Darryl
method 1: convert to string, then use/convert individual chars as needed
method 2: mod by 10 to get last digit, then divide by 10 to remove last digit, repeat till you get to 0

with method two you are getting the numbers in reverse order so I'd suggest you use a deque and push_front() to automagically arrange them
• 02-27-2006
swoopy
>I have a question. I want to break up an integer by digits.
One way is to use the modulus (%) operator and division (/) operator in a loop:
Code:

```//loop   digit = num % 10;   num = num / 10; //end loop```
• 02-27-2006
Magos
Modulus and division, modulus by 10 gets the last digit. Divide by 10 to cut off the last digit:
Code:

```std::list<int> List; while(Number > 0) {   List.push_front(Number % 10);   Number /= 10; }```
This will add all digits to the list.
• 02-27-2006
comwiz
alright, got it now, thanks for the help