Start learning free

LESSON 54 OF 338 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in Go: 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 integral division

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

    GO · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in Go, explained

    Problem overview

    You are given a non-negative integer n with at least two digits. The function lastTwoDigitSum returns the sum of its last two digits.

    For n = 2379, the last two digits are 7 and 9, and 7 + 9 = 16.

    Approach

    You could turn the number into text and pick out characters, but arithmetic is simpler. Two operators do the work:

    • The remainder operator %. n % 10 is the last digit of n, and n % 100 is the number formed by its last two digits.
    • Integer division /. When both operands are integers, Go drops the fractional part, so dividing a two-digit number by 10 gives its first digit.

    For 2379: 2379 % 100 is 79. Then 79 % 10 is 9 and 79 / 10 is 7. Add them to get 16.

    Working on lastTwoDigits rather than on n keeps the later steps simple: the value is always between 0 and 99, so one division by 10 is enough to separate its two digits, no matter how long n is.

    Step by step

    1. Isolate the last two digits. Declare lastTwoDigits := n % 100. Everything above the tens place is gone, and the value is between 0 and 99.
    2. Take the last digit. lastDigit := lastTwoDigits % 10 is the ones digit of 79, which is 9.
    3. Take the second-to-last digit. secondLastDigit := lastTwoDigits / 10. Dividing by 10 discards the ones digit, and integer division keeps just the whole part: 79 / 10 is 7.
    4. Add and return. The two digits are separate numbers now, so a plain + sums them.

    The solution in Go

    func lastTwoDigitSum(n int) int {
        lastTwoDigits := n % 100
        lastDigit := lastTwoDigits % 10
        secondLastDigit := lastTwoDigits / 10
        return lastDigit + secondLastDigit
    }
    

    Some Go details behind these lines:

    • := declares and infers. Each variable is an int, because n is an int and so are the constants.
    • Integer division truncates toward zero. For non-negative numbers, like the ones in this task, that is the same as rounding down. For negative numbers Go behaves differently from some other languages: -7 / 2 is -3 and -7 % 2 is -1, since the remainder takes the sign of the dividend. The task promises a non-negative n, so you do not have to handle that.
    • No conversion to a string. Working with numbers avoids slow and fiddly string handling.

    Trace one more value through the code, n = 1005. First, 1005 % 100 is 5, so lastTwoDigits is 5. Next, 5 % 10 is 5, which becomes lastDigit. Then 5 / 10 is 0, which becomes secondLastDigit. The sum is 5. The tens digit really is zero, and the code handles that without any special case.

    The same result also fits on one line, as n%10 + n/10%10. Splitting it into named steps, as the lesson does, makes each step easy to read and to check.

    Complexity

    The function does a fixed number of operations, so time is O(1) and space is O(1). The number of digits in n does not matter, because % reads only the end of the number.

    Edge cases and common mistakes

    • Exactly two digits. lastTwoDigitSum(45) gives 9: 45 % 100 is 45, the digits are 5 and 4.
    • Zeros in the last two digits. lastTwoDigitSum(100) gives 0, and lastTwoDigitSum(1005) gives 5. Here lastTwoDigits is 5, so secondLastDigit is 0, which is correct: the tens digit really is zero.
    • Mixing up % and /. n / 100 removes the last two digits instead of keeping them.
    • Dividing the original n by 10. n / 10 is 237 for 2379, not a digit. Divide lastTwoDigits, which is always below 100.
    • Unused variables. If you declare a variable and do not use it, Go refuses to compile.

    Small inputs to try: lastTwoDigitSum(2379) gives 16, lastTwoDigitSum(45) gives 9, lastTwoDigitSum(100) gives 0 and lastTwoDigitSum(1005) gives 5.

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free