4

Length is always 3, examples:

000
056
999

How can I get the number so I can use the value of the single digit in an array?

Would it be easier to convert to an char, loop through it and convert back to int when needed?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
user869778
  • 243
  • 1
  • 3
  • 7
  • Similar questions have been asked many times and a simple google search turns up plenty of good answers. – Austin Henley Oct 29 '11 at 02:01
  • duplicate of: [Getting each individual digit from a whole integer](http://stackoverflow.com/questions/3118490/getting-each-individual-digit-from-a-whole-integer) – AusCBloke Oct 29 '11 at 02:11
  • What form are these numbers have when you get them? Values of type `int`? Character strings? – Keith Thompson Oct 29 '11 at 02:38

2 Answers2

6

To get the decimal digit at position p of an integer i you would do this:

(i / pow(p, 10)) % 10;

So to loop from the last digit to the first digit, you would do this:

int n = 56; // 056
int digit;    

while(n) {
    digit = n % 10;
    n /= 10;

    // Do something with digit
}

Easy to modify to do it exactly 3 times.

Ry-
  • 218,210
  • 55
  • 464
  • 476
-3

As Austin says, you can easily find answers on Google. But it basically involves mod-by-10, then divide-by-ten. Assuming integers.

FeifanZ
  • 16,250
  • 7
  • 45
  • 84