How do you find the rightmost digit of an integer?

math

Solution

`x % 10` looks like the correct answer. But is not.

`-2 % 10` is either `-2` or `8` depending on language/implementation/whatever. And neither is actually "last digit".

So the correct answer is `abs(x) % 10`.

Problem

Assume that an int variable `x` that has already been declared, write an expression whose value is the last (rightmost) digit of `x`. I know the answer is `x%10`, but why is that the expression that reveals the rightmost digit?

Original source