Start learning free

LESSON 52 OF 347 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in C#: Using % and / on Integers

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

    C# · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in C#, explained

    Problem overview

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

    You could convert the number to a string and read characters, but arithmetic is simpler and needs no conversion. Two integer operators do the work:

    • %, the remainder operator, gives what is left after a division. n % 10 is the last digit and n % 100 is the number made of the last two digits.
    • / applied to two int values is an integer division: it drops the fractional part instead of producing a decimal. 79 / 10 is 7, not 7.9.

    Together they let you take a number apart digit by digit. Dividing by 10 removes the last digit, and the remainder after dividing by 10 is exactly that last digit.

    Step by step

    Follow n = 2379:

    1. Keep the last two digits. lastTwoDigits = n % 100 gives 79. Everything above the tens place is discarded.
    2. Take the last digit. lastDigit = lastTwoDigits % 10 gives 9.
    3. Take the other digit. secondLastDigit = lastTwoDigits / 10 gives 7. Dividing by 10 shifts the number one digit to the right, and integer division discards the leftover 9.
    4. Add them. 9 + 7 = 16, which is the value returned.

    The solution in C#

    class Solution {
        public int LastTwoDigitSum(int n) {
            int lastTwoDigits = n % 100;
            int lastDigit = lastTwoDigits % 10;
            int secondLastDigit = lastTwoDigits / 10;
            return lastDigit + secondLastDigit;
        }
    }
    

    A few things about the C# here:

    • All three variables are int. Because both operands of / are integers, lastTwoDigits / 10 is an integer too, so it fits in an int variable with no cast.
    • If lastTwoDigits were a double, the division would give 7.9 and the digit would be lost. Staying in int is what makes the division drop the fraction.
    • For a non-negative n, % always gives a result from 0 up to one less than the divisor.

    Why the intermediate variables help

    The same answer fits on one line: return n % 10 + n / 10 % 10;. That works because n / 10 drops the last digit, so taking % 10 of it gives the second-to-last digit. The one-liner is correct but dense, and operator precedence is easy to misread. The lesson's version names each value, so you can check each line against the example by hand. If a result is wrong, you can see which step went wrong. Both versions give the same answer for every non-negative n.

    Complexity

    There are three arithmetic operations and an addition, whatever the size of n. Time is O(1) and extra space is O(1).

    Edge cases and common mistakes

    • A zero in the tens place. For n = 2305, n % 100 is 5, not 05, since an int has no leading zeros. Then 5 % 10 is 5 and 5 / 10 is 0, so the sum is 5. The formula still works.
    • Exactly two digits. For n = 99, n % 100 is 99 and the answer is 18.
    • Trailing zeros. LastTwoDigitSum(100) returns 0 and LastTwoDigitSum(10) returns 1.
    • Negative numbers. The problem says non-negative, and that matters. In C#, the result of % takes the sign of the left operand, so -7 % 3 is -1. A negative n would give negative "digits". Integer division also truncates toward zero, so -7 / 2 is -3.
    • Mixing up the operators. Using / 100 gives the leading digits, not the last ones.

    Testing

    • LastTwoDigitSum(2379) returns 16.
    • LastTwoDigitSum(99) returns 18.
    • LastTwoDigitSum(100) returns 0.
    • LastTwoDigitSum(10) returns 1.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free