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
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
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
Last edited by Darryl; 02-27-2006 at 03:33 PM.
>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
Modulus and division, modulus by 10 gets the last digit. Divide by 10 to cut off the last digit:
This will add all digits to the list.Code:std::list<int> List; while(Number > 0) { List.push_front(Number % 10); Number /= 10; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
alright, got it now, thanks for the help