Start learning free

LESSON 53 OF 414 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in Scala: Modulo and Integer Division

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

    SCALA · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in Scala, explained

    Problem overview

    Given a non-negative integer n with at least two digits, the method 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

    Treating the number as text and picking out characters would work, but arithmetic is simpler. Two operators do all the work:

    • % 10 gives the last digit: 2379 % 10 is 9.
    • / 10 between two integers drops the last digit: 2379 / 10 is 237.

    The modulo operator % returns the remainder of a division. Dividing by 100 and keeping the remainder keeps the last two digits as a number: 2379 % 100 is 79. From a two-digit number you can read both digits the same way: the remainder after dividing by 10 is the units digit, and the quotient is the tens digit. For 79, 79 % 10 is 9 and 79 / 10 is 7.

    Why the digits come out right

    Our numbers are written in base 10, so dividing by 10 moves every digit one place to the right, and the digit that falls off the end is the remainder. Dividing by 100 does the same for two places. That is why % 100 keeps exactly the last two digits and discards everything to their left, however long the number is.

    Step by step

    1. Isolate the last two digits. Declare a val named lastTwoDigits as n % 100. For 2379 it holds 79.
    2. Take the last digit. Declare lastDigit as lastTwoDigits % 10, which is 9.
    3. Take the second-to-last digit. Declare secondLastDigit as lastTwoDigits / 10, which is 7. Both operands are Int, so the division keeps only the whole part.
    4. Add them and return the sum: 9 + 7 = 16.

    The solution in Scala

    class Solution {
        def lastTwoDigitSum(n: Int): Int = {
            val lastTwoDigits = n % 100
            val lastDigit = lastTwoDigits % 10
            val secondLastDigit = lastTwoDigits / 10
            return lastDigit + secondLastDigit
        }
    }
    

    Scala details:

    • val for each step. Each digit is computed once and never changes, so an immutable val fits. Scala infers the type Int in every case.
    • Integer division. Dividing an Int by an Int gives an Int with the fraction dropped. If either side were a Double, 79 / 10 would be 7.9.
    • Negative numbers. In Scala, the sign of % follows the left operand, so -2379 % 100 is -79. The problem promises a non-negative n, so this doesn't arise here, but it matters if you reuse the idea.
    • return is optional. The final expression lastDigit + secondLastDigit is the block's value on its own.

    Complexity

    Time: O(1). Three arithmetic operations and one addition, whatever the size of n.

    Space: O(1). Three integers.

    Edge cases and common mistakes

    • Zeros in the last digits. lastTwoDigitSum(100) is 0: the last two digits are 0 and 0. lastTwoDigitSum(10) is 1, because the number 10 has digits 1 and 0.
    • A zero tens digit. For n = 2305, n % 100 is 5, the digits are 0 and 5, and the sum is 5. The same steps work: 5 % 10 is 5 and 5 / 10 is 0.
    • A single digit. The problem promises two digits at least. The formula still works for one digit: lastTwoDigitSum(7) returns 7.
    • Mixing up the operators. % keeps the remainder and / keeps the quotient. Using / 100 would return the leading digits instead of the trailing ones.
    • Adding the wrong values. Returning lastTwoDigits alone gives 79, not 16. The method needs the sum of the two digits.

    Testing

    • n = 2379 gives 16.
    • n = 100 gives 0.
    • n = 10 gives 1.
    • n = 99 gives 18.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free