Start learning free

LESSON 54 OF 410 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in Python: Using % and //

Practice Read the full explanation

Given a non-negative integer n with at least two digits, compute and return the sum of the last two digits

Example:

Input: n = 2379

Output: 16

Explanation:
Last two digits of n are 7 and 9, so our answer is 7 + 9 = 16
Hints:

You can make use of modulo operator

You can get the last digit of a number using modulo 10:
2379 % 10 = 9

You can get a number consisting of last 2 digits using modulo 100:
2379 % 100 = 79

If you have a 2 digit number, you can get the first digit by using integer division

Divide a 2 digit number to 10 to get the first digit:
79 / 10 = 7 (integer division)
STEP 1 OF 4
    Ctrl + Enter to check

    PYTHON · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in Python, explained

    Problem overview

    The method lastTwoDigitSum receives a non-negative integer n with at least two digits. It returns the sum of the last two digits of n.

    For n = 2379 the last two digits are 7 and 9, so the answer is 7 + 9 = 16. The same idea answers the common question of how to get the last digit or the last two digits of a number in Python, and the lesson does it with two operators and no strings.

    Approach: modulo takes digits from the right

    The remainder operator % returns what is left after a division. Dividing by 10 leaves the last digit, and dividing by 100 leaves the last two:

    n = 2379
    print(n % 10)   # 9
    print(n % 100)  # 79
    

    That gives the number 79 in one operation. To split 79 into its two digits, use the same tool again for the ones digit (79 % 10 is 9) and floor division // for the tens digit (79 // 10 is 7). Floor division divides and throws away the remainder.

    The sum of the two is the answer. Every step is a small calculation on integers, so no conversion to text is needed.

    Step by step

    1. Keep the last two digits together in lastTwoDigits, using n % 100. Everything above the tens place is gone: 2379 becomes 79.
    2. Take the last digit of that number into lastDigit, with lastTwoDigits % 10. This gives 9.
    3. Take the other digit into secondLastDigit, with lastTwoDigits // 10. It must be //, because / always returns a float in Python and 79 / 10 is 7.9.
    4. Return the sum of the two variables. A plain + is enough, since each is already a single digit.

    The solution in Python

    class Solution:
        def lastTwoDigitSum(self, n: int) -> int:
            lastTwoDigits = n % 100
            lastDigit = lastTwoDigits % 10
            secondLastDigit = lastTwoDigits // 10
            return lastDigit + secondLastDigit
    

    Some Python details here:

    • % and // go together. For non-negative numbers, a // b is the quotient and a % b is the remainder, and divmod(79, 10) returns both at once as (7, 9).
    • The result of // on integers is an integer. The method is annotated to return an int, and floor division keeps it that way.
    • The same code works on huge numbers. Python integers have no fixed size, so a number with 30 digits still works.

    The string alternative

    You can also read the last two characters of the number as text. That is a valid way to see the digits, but it creates strings and needs conversions back to integers:

    n = 2379
    print(str(n)[-2:])                           # 79
    print(sum(int(c) for c in str(n)[-2:]))      # 16
    

    The arithmetic version is the habit to build, because the same % 10 and // 10 moves are used in many digit problems.

    Complexity

    Time: O(1). The method does four arithmetic operations. Python's integers can be arbitrarily large, so the first modulo technically scales with the number's size, but for numbers of normal size it is constant.

    Space: O(1). Three integer variables are used.

    Edge cases and common mistakes

    • Exactly two digits. For n = 99, 99 % 100 is 99, and the answer is 9 + 9 = 18. The modulo does nothing harmful when the number is already short.
    • Zeros in the tens place. For n = 105, the last two digits are 0 and 5, and 105 % 100 is 5, so secondLastDigit is 0 and the answer is 5. The string approach would give "05", which is also fine when each character is converted separately.
    • Trailing zeros. For n = 100, the result is 0.
    • Using / instead of //. 79 / 10 is 7.9, so the sum becomes 16.9 instead of 16.
    • Taking % 10 twice on the same number. n % 10 is the last digit, so doing it again gives the last digit again. The second-to-last digit needs a shift first: n // 10 % 10, or lastTwoDigits // 10 as in the lesson.
    • Negative inputs. The lesson only gives non-negative numbers. In Python, -2379 % 100 is 21, because the remainder takes the sign of the divisor, so negative inputs need abs(n) first.

    Testing

    • lastTwoDigitSum(2379) returns 16.
    • lastTwoDigitSum(99) returns 18.
    • lastTwoDigitSum(105) returns 5.
    • lastTwoDigitSum(100) returns 0.
    • lastTwoDigitSum(1000000007) returns 7.
    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free