Start learning free

LESSON 52 OF 410 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in Java: Using % and / on an int

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

    JAVA · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in Java, explained

    Problem overview

    The method lastTwoDigitSum receives a non-negative int named 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 16.

    No loop and no conversion to a string is needed. Two integer operators, the remainder % and the integer division /, are enough, and they also answer the common question of how to get the last digit of a number in Java.

    Approach: take digits with % and /

    Our number system is based on 10, which gives two rules that carry the whole solution:

    • n % 10 is the last digit. The remainder after dividing by 10 is whatever sits in the ones place: 2379 % 10 is 9.
    • n % 100 is the number formed by the last two digits. 2379 % 100 is 79.

    Once the number is reduced to its last two digits, 79, the two digits are separated with the same operators. The last digit is 79 % 10, which is 9. The first digit is 79 / 10, which is 7. When both operands are int, Java's / keeps only the whole part of the quotient and discards the remainder: 79 divided by 10 is 7.9, and the result is 7.

    Adding the two pieces, 9 + 7, gives 16.

    Step by step

    1. Declare lastTwoDigits as n % 100. This removes everything except the tens and ones places.
    2. Declare lastDigit as lastTwoDigits % 10. This is the ones digit of the original number.
    3. Declare secondLastDigit as lastTwoDigits / 10. Because lastTwoDigits is less than 100, dividing by 10 leaves exactly one digit: the tens digit of n.
    4. Return lastDigit + secondLastDigit. The two digits are ordinary int values, so a plain + adds them.

    The solution in Java

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

    Each name describes the value it holds, which makes the method easy to read, and every line does exactly one thing. If you prefer to write it in one line, the same result comes from n % 10 + n / 10 % 10. The operators % and / have the same precedence and are evaluated from left to right, so n / 10 % 10 means (n / 10) % 10: it drops the ones digit and then takes the new last digit. For 2379 that is 237 % 10, which is 7.

    Edge cases and common mistakes

    • A zero in the tens place. For n = 1005 the last two digits are 0 and 5. n % 100 is 5, then 5 / 10 is 0 and 5 % 10 is 5, so the answer is 5. The leading zero causes no problem, because the digits are computed as numbers.
    • Exactly 100. 100 % 100 is 0, so both digits are 0 and the answer is 0.
    • Two digits only. For n = 99 the answer is 18, since 99 % 100 is 99 itself.
    • A single digit. The task promises at least two digits, but the code still returns the digit itself for n = 5, because 5 / 10 is 0.
    • Negative input. In Java the sign of % follows the left operand, so -2379 % 100 is -79, and the method returns -16 instead of 16. The task rules out negative numbers. If you reuse this code on data that can be negative, apply Math.abs(n) first.
    • Forgetting that / truncates. Beginners often expect 79 / 10 to be 7.9. With two int operands the result is the int 7, and no rounding happens. If one operand were a double, the result would be 7.9.
    • Swapping the operators. n / 100 removes the last two digits, which is the opposite of what is wanted here. % keeps the low part of a number and / discards it.

    Summing all the digits

    The same two operators give the sum of every digit of a number, with a loop that peels off the last digit until nothing is left:

    static int digitSum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
    

    For 2379 the loop adds 9, 7, 3 and 2, and digitSum(2379) returns 21. The last-two-digits version is this loop run exactly twice.

    Testing

    Good inputs to try: 2379 gives 16, 1005 gives 5, 100 gives 0 and 99 gives 18.

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