I know the price is x%10, but why is that the expression the reveals the rightmost digit?

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

You are watching: Write an expression whose value is the last (rightmost) digit of x.

-2 % 10 is either -2 or 8 depending upon language/implementation/whatever. And also neither is in reality "last digit".

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

It"s Math

When you usage the "%" operator you are asking because that "What is the remainder after splitting by that number" for this reason if I have 28 % 10 I"m speak "What is the remainder of 28 divided by 10". This, that course, would give me 8. Gaining the remainder the a number divided by 10 just leaves you with the ones number (right many number).

We use a Decimal / basic 10 number system. So if you usage 10 friend will constantly get remainders in between 0-9.

In Java, "%" is the remainder operator and also works the method described above.

Summary of operator in Java

re-publishing
monitor
answered jan 24 "14 in ~ 7:49

RWhitenRWhiten
3

Thanks because that contributing response to stack Overflow!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them increase with references or an individual experience.

Draft saved

authorize up making use of Google
authorize up using Email and Password
send

Post together a guest

name
email Required, yet never shown

Post as a guest

surname
email

Required, but never shown

Not the prize you're looking for? Browse various other questions tagged math or ask your very own question.

The Overflow Blog
Featured ~ above Meta
related
367
just how to ring up the an outcome of essence division?
302
Why can't decimal number be represented exactly in binary?
36
just how do you publish the specific value that a floating point number?
1220
straightforward interview question gained harder: given numbers 1..100, discover the lacking number(s) given precisely k are absent
1132
how to execute an creature division, and also separately acquire the remainder, in JavaScript?
74
What is the probability that the array will remain the same?
195
how does the HyperLogLog algorithm work?
4
Why the bit operation ns & (-i) equals to rightmost bit?
hot Network concerns an ext hot concerns

concern feed

stack Overflow
assets
firm
ridge Exchange Network
site design / logo design © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.11.23.40817

Stack Overflow works ideal with JavaScript permitted