. Advertisement .
..3..
. Advertisement .
..4..
I’m learning about “write an expression whose value is the last (rightmost) digit of x. Assume that an int variable x that has already been declared”, honestly I don’t know anything about it. Please give me some knowledge about it.
Here are the responses to your inquiries.
“What is the leftover after division by that number” is what you are requesting when you employ the “percent” operator. I would ask “What is the remaining of 28 divided by 10” if I had a percentage of 28 10. Of course, this would give me 8. Finding the residual of a number after dividing it by ten only gives the ones digit (right most number).
Decimal/Base 10 is the numbering system we employ. Therefore, if you use 10, you will always receive remainders between 0 and 9.
“Percent” is the remainder operator in Java and operates as previously mentioned.
x % 10
seems like the right answer. It isn’t.-2 % 10
is either-2
or8
depending on language/implementation/whatever. Both are not the “last digit”.The correct answer is
abs(x) % 10
.