Hey all,

I have a question. I want to break up an integer by digits. For example,

Does anybody know if this is possible to do.Code:`1234`

1

2

3

4

Thanks,

Kyle

Printable View

- 02-27-2006comwizQuick Question
Hey all,

I have a question. I want to break up an integer by digits. For example,

Code:`1234`

1

2

3

4

Thanks,

Kyle - 02-27-2006Darryl
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-2006swoopy
>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-2006Magos
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;

}

- 02-27-2006comwiz
alright, got it now, thanks for the help