I understand the prize is x%10, however why is the the expression that reveals the rightmost digit?

x % 10 looks like the exactly answer. However is not.

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

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

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

It"s Math

When you use the "%" operator you space asking for "What is the remainder after dividing by that number" so if I have actually 28 % 10 I"m saying "What is the remainder that 28 separated by 10". This, of course, would provide me 8. Gaining the remainder the a number separated by 10 just leaves you v the ones number (right most number).

We use a Decimal / basic 10 number system. For this reason if you usage 10 girlfriend will always get remainders in between 0-9.

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

Summary of operators in Java

share

improve this price

monitor

answered jan 24 "14 in ~ 7:49

RWhitenRWhiten

15366 bronze title

3

add a comment |

## her Answer

Thanks for contributing solution to ridge Overflow!

Please be sure to*answer the question*. Administer details and also share her research!

But *avoid* …

To learn more, view our tips on writing good answers.

See more: What Does Strength Do In Nba 2K17, Attribute Testing

Draft saved

Draft discarded

### Sign increase or log in in

sign up making use of Google

authorize up using Facebook

authorize up using Email and Password

submit

### Post together a guest

surname

email Required, however never shown

### Post as a guest

name

Required, but never shown

write-up Your prize Discard

By clicking “Post her Answer”, friend agree come our regards to service, privacy policy and also cookie plan

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

The Overflow Blog

Featured on Meta

connected

362

how to round up the an outcome of integer division?

300

Why can't decimal numbers be represented exactly in binary?

36

exactly how do you print the precise value of a floating point number?

1209

straightforward interview question gained harder: provided numbers 1..100, uncover the absent number(s) given exactly k are missing

1114

just how to perform an creature division, and also separately obtain the remainder, in JavaScript?

193

just how does the HyperLogLog algorithm work?

2

Why the little bit operation i & (-i) equals to rightmost bit?

47

just how to ring to nearest also integer?

warm Network questions more hot concerns

concern feed

subscribe to RSS

question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

stack Overflow

products

agency

ridge Exchange Network

site style / logo © 2021 ridge Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.9.21.40254

Stack Overflow works best with JavaScript permitted

her privacy

By click “Accept all cookies”, girlfriend agree ridge Exchange deserve to store cookies on your machine and disclose information in accordance v our Cookie Policy.