Problem overview
The method lastTwoDigitSum receives a non-negative int named n with at least two digits and returns the sum of its last two digits. For n = 2379 the last two digits are 7 and 9, so the answer is 16.
No loop and no conversion to a string is needed. Two integer operators, the remainder % and the integer division /, are enough, and they also answer the common question of how to get the last digit of a number in Java.
Approach: take digits with % and /
Our number system is based on 10, which gives two rules that carry the whole solution:
n % 10is the last digit. The remainder after dividing by 10 is whatever sits in the ones place:2379 % 10is 9.n % 100is the number formed by the last two digits.2379 % 100is 79.
Once the number is reduced to its last two digits, 79, the two digits are separated with the same operators. The last digit is 79 % 10, which is 9. The first digit is 79 / 10, which is 7. When both operands are int, Java's / keeps only the whole part of the quotient and discards the remainder: 79 divided by 10 is 7.9, and the result is 7.
Adding the two pieces, 9 + 7, gives 16.
Step by step
- Declare
lastTwoDigitsasn % 100. This removes everything except the tens and ones places. - Declare
lastDigitaslastTwoDigits % 10. This is the ones digit of the original number. - Declare
secondLastDigitaslastTwoDigits / 10. BecauselastTwoDigitsis less than 100, dividing by 10 leaves exactly one digit: the tens digit ofn. - Return
lastDigit + secondLastDigit. The two digits are ordinaryintvalues, so a plain+adds them.
The solution in Java
class Solution {
public int lastTwoDigitSum(int n) {
int lastTwoDigits = n % 100;
int lastDigit = lastTwoDigits % 10;
int secondLastDigit = lastTwoDigits / 10;
return lastDigit + secondLastDigit;
}
}
Each name describes the value it holds, which makes the method easy to read, and every line does exactly one thing. If you prefer to write it in one line, the same result comes from n % 10 + n / 10 % 10. The operators % and / have the same precedence and are evaluated from left to right, so n / 10 % 10 means (n / 10) % 10: it drops the ones digit and then takes the new last digit. For 2379 that is 237 % 10, which is 7.
Edge cases and common mistakes
- A zero in the tens place. For
n = 1005the last two digits are 0 and 5.n % 100is 5, then5 / 10is 0 and5 % 10is 5, so the answer is 5. The leading zero causes no problem, because the digits are computed as numbers. - Exactly 100.
100 % 100is 0, so both digits are 0 and the answer is 0. - Two digits only. For
n = 99the answer is 18, since99 % 100is 99 itself. - A single digit. The task promises at least two digits, but the code still returns the digit itself for
n = 5, because5 / 10is 0. - Negative input. In Java the sign of
%follows the left operand, so-2379 % 100is -79, and the method returns -16 instead of 16. The task rules out negative numbers. If you reuse this code on data that can be negative, applyMath.abs(n)first. - Forgetting that
/truncates. Beginners often expect79 / 10to be 7.9. With twointoperands the result is theint7, and no rounding happens. If one operand were adouble, the result would be 7.9. - Swapping the operators.
n / 100removes the last two digits, which is the opposite of what is wanted here.%keeps the low part of a number and/discards it.
Summing all the digits
The same two operators give the sum of every digit of a number, with a loop that peels off the last digit until nothing is left:
static int digitSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
For 2379 the loop adds 9, 7, 3 and 2, and digitSum(2379) returns 21. The last-two-digits version is this loop run exactly twice.
Testing
Good inputs to try: 2379 gives 16, 1005 gives 5, 100 gives 0 and 99 gives 18.
