How do I get what the digits of a number are in C++ without converting it to strings or character arrays?
simple recursion:
#include // 0-based index pos int getDigit (const long number, int pos) { return (pos == 0) ? number % 10 : getDigit (number/10, --pos); } int main (void) { std::cout << getDigit (1234567, 4) << "\n"; }