Problem overview
Given a non-negative integer n with at least two digits, LastTwoDigitSum returns the sum of its last two digits. For n = 2379 the last two digits are 7 and 9, so the answer is 16.
Approach
You could convert the number to a string and read characters, but arithmetic is simpler and needs no conversion. Two integer operators do the work:
%, the remainder operator, gives what is left after a division.n % 10is the last digit andn % 100is the number made of the last two digits./applied to twointvalues is an integer division: it drops the fractional part instead of producing a decimal.79 / 10is7, not7.9.
Together they let you take a number apart digit by digit. Dividing by 10 removes the last digit, and the remainder after dividing by 10 is exactly that last digit.
Step by step
Follow n = 2379:
- Keep the last two digits.
lastTwoDigits = n % 100gives 79. Everything above the tens place is discarded. - Take the last digit.
lastDigit = lastTwoDigits % 10gives 9. - Take the other digit.
secondLastDigit = lastTwoDigits / 10gives 7. Dividing by 10 shifts the number one digit to the right, and integer division discards the leftover 9. - Add them. 9 + 7 = 16, which is the value returned.
The solution in C#
class Solution {
public int LastTwoDigitSum(int n) {
int lastTwoDigits = n % 100;
int lastDigit = lastTwoDigits % 10;
int secondLastDigit = lastTwoDigits / 10;
return lastDigit + secondLastDigit;
}
}
A few things about the C# here:
- All three variables are
int. Because both operands of/are integers,lastTwoDigits / 10is an integer too, so it fits in anintvariable with no cast. - If
lastTwoDigitswere adouble, the division would give7.9and the digit would be lost. Staying inintis what makes the division drop the fraction. - For a non-negative
n,%always gives a result from 0 up to one less than the divisor.
Why the intermediate variables help
The same answer fits on one line: return n % 10 + n / 10 % 10;. That works because n / 10 drops the last digit, so taking % 10 of it gives the second-to-last digit. The one-liner is correct but dense, and operator precedence is easy to misread. The lesson's version names each value, so you can check each line against the example by hand. If a result is wrong, you can see which step went wrong. Both versions give the same answer for every non-negative n.
Complexity
There are three arithmetic operations and an addition, whatever the size of n. Time is O(1) and extra space is O(1).
Edge cases and common mistakes
- A zero in the tens place. For
n = 2305,n % 100is5, not05, since aninthas no leading zeros. Then5 % 10is 5 and5 / 10is 0, so the sum is 5. The formula still works. - Exactly two digits. For
n = 99,n % 100is 99 and the answer is 18. - Trailing zeros.
LastTwoDigitSum(100)returns 0 andLastTwoDigitSum(10)returns 1. - Negative numbers. The problem says non-negative, and that matters. In C#, the result of
%takes the sign of the left operand, so-7 % 3is-1. A negativenwould give negative "digits". Integer division also truncates toward zero, so-7 / 2is-3. - Mixing up the operators. Using
/ 100gives the leading digits, not the last ones.
Testing
LastTwoDigitSum(2379)returns16.LastTwoDigitSum(99)returns18.LastTwoDigitSum(100)returns0.LastTwoDigitSum(10)returns1.