Start learning free

LESSON 56 OF 414 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in Rust: Remainder and 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 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

    RUST · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in Rust, explained

    Problem overview

    The function last_two_digit_sum 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.

    No loop and no conversion to text is needed. Two arithmetic operators, % (remainder) and / (division), pull a number apart digit by digit.

    Approach

    Our number system is base 10, so the remainder of a division by 10 is the last digit, and the remainder of a division by 100 is the number formed by the last two digits:

    • 2379 % 10 is 9.
    • 2379 % 100 is 79.

    Once the problem is reduced to the two-digit number 79, the digits are easy to separate. Its last digit is 79 % 10 = 9. Its first digit is 79 / 10 = 7, because dividing two integers in Rust gives an integer and drops the fractional part. Adding the two gives 16.

    Step by step

    1. Keep only the last two digits. Take n % 100 and store it in last_two_digits. Everything above the tens place disappears.
    2. Extract the last digit. Take last_two_digits % 10 and store it in last_digit.
    3. Extract the second-to-last digit. Take last_two_digits / 10, which is the tens digit of n, and store it in second_last_digit.
    4. Add the two digits and return the sum.

    The solution in Rust

    impl Solution {
        fn last_two_digit_sum(n: i32) -> i32 {
            let last_two_digits = n % 100;
            let last_digit = last_two_digits % 10;
            let second_last_digit = last_two_digits / 10;
            return last_digit + second_last_digit;
        }
    }
    

    A few Rust details in this short function:

    • Types are inferred. The parameter n is an i32, so 100 and 10 are treated as i32 too, and so are all three let variables. You never have to write their types.
    • Integer division truncates. With two integers, / keeps only the whole part: 79 / 10 is 7, not 7.5. Rust never converts an integer division to a decimal on its own, and it refuses to mix i32 and f64 in one expression.
    • let variables can't be reassigned. Each of the three is computed once and never changes, so no mut is needed.
    • return with a semicolon is valid. The usual Rust style drops the keyword and the semicolon and ends the function with the bare expression last_digit + second_last_digit, which gives the same result.

    Complexity

    The function does a fixed number of arithmetic operations whatever the size of n, so it runs in O(1) time and uses O(1) extra space.

    Edge cases and common mistakes

    • Zeros in the last two digits. For n = 100, the remainder by 100 is 0, so both digits are 0 and the sum is 0. For n = 1005, 1005 % 100 is 5, the digits are 0 and 5, and the sum is 5. The tens digit comes out as 5 / 10 = 0, which is correct.
    • Exactly two digits. For n = 99, 99 % 100 is 99 and the answer is 18. The remainder step changes nothing, and that is fine.
    • The largest i32. For 2147483647 the last two digits are 4 and 7, and the sum is 11. The remainder is always below 100, so the addition can never overflow.
    • Dividing by 10 on the whole number. n / 10 drops the last digit but leaves all the others. To get the tens digit, divide last_two_digits, not n.
    • Negative numbers. The lesson only gives non-negative inputs. In Rust, % takes the sign of the dividend, so -2379 % 100 is -79, and -79 / 10 is -7 because integer division truncates toward zero. The function would then return a negative sum, which is why the non-negative condition matters.

    Testing

    Try these inputs in the editor above: 2379 should give 16, 100 should give 0, 1005 should give 5, and 99 should give 18.

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