cmathnumbersdigitsdigit

C: how to break apart a multi digit number into separate variables?


Say I have a multi-digit integer in C. I want to break it up into single-digit integers.

123 would turn into 1, 2, and 3.

How can I do this, especially if I don't know how many digits the integer has?


Solution

  • int value = 123;
    while (value > 0) {
     int digit = value % 10;
     // do something with digit
     value /= 10;
    }