Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


Given any number, how can we retrieve each digit of that number?

i.e. For number = 4321, how can I get individual digits 4,3,2 and 1?

share|cite|improve this question
How are you given the number? In practice, e.g. in computer science applications, you are sometimes already given the number as a string and you can just extract the characters directly. – Qiaochu Yuan Nov 13 '10 at 21:55
up vote 4 down vote accepted

HINT $\rm\displaystyle\quad 3\ =\ \bigg\lfloor \frac{4321}{100}\bigg\rfloor \ mod\ 10 \ =\ 43\ mod\ 10\ \ $ i.e. rightshift then chop off least significant digit.

Or, if you need all digits "unwind" the Horner representation $\rm\ N\ =\ d_0 + 10\ (d_1 + 10\ (d_2 + \:\cdots\: ))\:$ by using division by 10 with remainder, viz $\rm\ d_0 = N\ mod\ 10,\ \ d_1 = (N-d_0)/10\ mod\ 10,\ \ldots$

share|cite|improve this answer

1) Take N modulo 10. 2) Set N = [N/10] ([] is the greatest integer funtion) 3) GO back to step 1) until N = 0.

I hope I haven't misunderstood your question here.

share|cite|improve this answer

For the k'th digit (counting left from the unit's place), divide the number by $10^k$ and call the remainder r. Then, $[r/(10^{k-1})]$ gives the k'th digit.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.