Equation that can easily be changed to output the digit in 1's, 10's ,100's etc?

51 Views Asked by At

I need an equation that can be easily changed to output the digit which is held in the 1's slot, 10's slot, 100's slot, etc.

EX. I want the 100's digit in 6810 EX2. I want the 1's digit in 29115

What the equation should do is that once it is done calculating, all that is left is the selected "slot" or whatever it is called, nothing else, no other numbers, just the number in the slot I wanted

It would help if the equation was really simple, but anything helps

3

There are 3 best solutions below

0
On

If you want the $10^k$'s digit of $n$, you should compute $\left\lfloor\dfrac{n}{10^k}\right\rfloor \pmod{10}$.

Ex1: If you want the $100 = 10^2$ digit of $6810$, then $\left\lfloor\dfrac{6810}{10^2}\right\rfloor = \lfloor 68.1\rfloor = 68 \equiv \boxed{8} \pmod{10}$.

Ex2: If you want the $1 = 10^0$ digit of $29115$, then $\left\lfloor\dfrac{29115}{10^0}\right\rfloor = \lfloor 29115\rfloor = 29115 \equiv \boxed{5} \pmod{10}$.

0
On

To get the one's digit of $x$: $$f(x)=((x\mod 10)-(x\mod 1))/1$$ To get the ten's digit of $x$: $$f(x)=((x\mod 100)-(x\mod 10))/10$$ To get the hundred's digit of $x$: $$f(x)=((x\mod 1000) - (x\mod 100))/100$$ and so on.

0
On

Suppose you want the digit of the nonnegative integer $x$ corresponding to the power $10^n$ (so $n$ can be $0,1,2,\ldots$). This is just

$$\left\lfloor\frac{x}{10^n}\right\rfloor - 10\left\lfloor\frac{\left\lfloor\frac{x}{10^n}\right\rfloor}{10}\right\rfloor$$