How do I get what the digits of a number are in C++ without converting it to strings or character arrays?
I have seen many answers, but they all forgot to use do {...} while()
loop, which is actually the canonical way to solve this problem and handle 0
properly.
My solution is based on this one by Naveen.
int n = 0;
std::cin>>n;
std::deque digits;
n = abs(n);
do {
digits.push_front( n % 10);
n /= 10;
} while (n>0);