How do you find the rightmost digit of an integer?

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.

Leave a Comment