How to get the digits of a number without converting it to a string/ char array?

前端 未结 15 1249
既然无缘
既然无缘 2020-11-27 04:07

How do I get what the digits of a number are in C++ without converting it to strings or character arrays?

15条回答
  •  一整个雨季
    2020-11-27 04:59

    The following prints the digits in order of ascending significance (i.e. units, then tens, etc.):

    do {
        int digit = n % 10;
        putchar('0' + digit);
        n /= 10;
    } while (n > 0);
    

提交回复
热议问题