Start learning free

LESSON 53 OF 348 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in Kotlin: 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

    KOTLIN · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in Kotlin, explained

    Problem overview

    The function lastTwoDigitSum receives a non-negative integer 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 7 + 9 = 16.

    The task has no string handling in it. Two arithmetic operators, remainder and integer division, pull the digits out of the number directly.

    Approach

    Two facts about decimal numbers do the work:

    • Dividing by 10 and keeping the remainder gives the last digit. 2379 % 10 is 9.
    • Dividing by 100 and keeping the remainder gives the last two digits as one number. 2379 % 100 is 79.

    So the plan is to cut the number down to its last two digits, 79, and then split that two-digit number into its tens digit and its ones digit. The ones digit is 79 % 10 = 9. The tens digit is 79 / 10 = 7, because dividing integers drops the fractional part and with it the ones digit.

    Step by step

    1. Cut the number to its last two digits. Declare lastTwoDigits as n % 100. Everything above the hundreds place disappears.
    2. Take the last digit. Declare lastDigit as lastTwoDigits % 10.
    3. Take the second-to-last digit. Declare secondLastDigit as lastTwoDigits / 10. Both operands are integers, so Kotlin's / keeps only the whole part: 79 / 10 is 7, not 7.9.
    4. Add them and return the sum. The two digits are ordinary Int values, so + adds them.

    All three variables are declared with val, because each one is set once and never changes.

    The solution in Kotlin

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

    Details worth knowing:

    • % is the remainder operator. In Kotlin it works on Int values and gives what is left after a division. It is the same idea as the modulo operator in other languages, with one caveat below.
    • / on two Int values is integer division. There is no rounding up and no decimal point. To get 7.9 you would need a Double.
    • Inferred types. No variable has a written type. Kotlin infers Int for each one.
    • Intermediate names. The same result fits in one line: n % 10 + n / 10 % 10. The named steps follow how you would do it on paper.

    Complexity

    The function does a few arithmetic operations, whatever the size of n. Time is O(1) and extra space is O(1). Converting the number to a string and reading its last two characters would also work, but it creates a string and is unnecessary here.

    Edge cases and common mistakes

    • Zeros among the last digits. For n = 100 the last two digits are 00, so lastTwoDigits is 0 and the sum is 0. For n = 305 the last two digits are 05: 305 % 100 is 5, the last digit is 5, the tens digit is 0, and the sum is 5. The leading zero causes no trouble, because the number is never written out as text.
    • Exactly two digits. For n = 99, the remainder by 100 is 99 itself, and the sum is 18.
    • Dividing by 100 instead of taking the remainder. n / 100 removes the last two digits rather than keeping them.
    • Taking n % 10 and n / 10 directly. For 2379, n / 10 is 237, a three-digit number, not the second-to-last digit. The first % 100 is what makes the second step work.
    • Negative numbers. The challenge only gives non-negative values. In Kotlin, the remainder takes the sign of the left operand, so for -2379 this code computes -79, then -9 and -7, and returns -16.

    Testing

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