Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

All,

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|improve this question
1  
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
add comment

3 Answers 3

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|improve this answer
add comment

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|improve this answer
add comment

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|improve this answer
add comment

Your Answer

 
discard

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.