94.5k views
4 votes
Suppose you have an int variable called number. What Java expression produces the last digit of the number (the 1s place)? (Our code checker won't match every possible expression; come up with the simplest expression using division and modulus.)

User Rolandl
by
5.6k points

2 Answers

4 votes

Answer:

Java - number % 10;

Step-by-step explanation:

The modulo (%) or remainder operator in Java and other programming languages is used to get the remainder value from a division operation.

Dividing any integer by 10 moves the decimal point one point left, e.g. 158 becomes 15.8.

The number after the point is the result you'd get from the modulo operation, which would be the last digit of an integer before division. If you use the modulo operator on a multiplier of 10, you'd get 0, which is also correct, e.g. 100 % 10 = 0.

User Feidtmb
by
4.7k points
5 votes

Answer:

number % 10

Step-by-step explanation:

the % (modulus) operator returns the remainder of a division operation

13 % 10 would return 3

95 % 10 would return 5

17 % 5 would return 2

using the modulus operation by 10 on any number would give the reminder of dividing that number by 10 ie, the last digit.

User Doom
by
5.6k points