Start learning free

LESSON 53 OF 415 · CALCULATIONS - CHALLENGES

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

    SWIFT · CALCULATIONS - CHALLENGES

    Last Two Digit Sum in Swift, explained

    Problem overview

    The method 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 16.

    Approach

    You could turn the number into text and read the last two characters, but that is roundabout. Two integer operators do the job directly:

    • 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.
    • Integer division / between two Int values drops the fraction. 79 / 10 is 7, the tens digit.

    For 2379: 2379 % 100 is 79, then 79 % 10 is 9 and 79 / 10 is 7. Adding them gives 16.

    Step by step

    1. Cut the number down to two digits. Declare lastTwoDigits as n % 100. Everything above the last two digits is discarded.
    2. Get the ones digit. Declare lastDigit as lastTwoDigits % 10.
    3. Get the tens digit. Declare secondLastDigit as lastTwoDigits / 10. Dividing a two-digit number by 10 removes the ones digit, and because both operands are integers there is no fraction to remove.
    4. Add and return. The two digits are ordinary integers, so lastDigit + secondLastDigit is the answer.

    The solution in Swift

    class Solution {
        func lastTwoDigitSum(_ n: Int) -> Int {
            let lastTwoDigits = n % 100
            let lastDigit = lastTwoDigits % 10
            let secondLastDigit = lastTwoDigits / 10
            return lastDigit + secondLastDigit
        }
    }
    

    Swift details worth knowing:

    • The remainder operator. Swift's % works on integers and takes the sign of the left operand: -7 % 3 is -1. The lesson's inputs are non-negative, so the sign never matters here.
    • Integer division truncates toward zero. 7 / 2 is 3, not 3.5. This only happens because both operands are Int.
    • Constants. Each digit is computed once, so let is the right keyword.
    • Inferred types. The constants are Int because the expressions on the right are Int.

    Why the remainder trick works

    Our numbers are written in base 10, so each place is worth ten times the one to its right. Dividing by 10 shifts every digit one place to the right and drops the last one. The remainder after dividing by 10 is exactly that dropped digit. The same idea with 100 keeps the last two places. This is why % 10 and / 10 show up in almost every digit problem, such as finding the digits of a number one by one or checking whether a number is even.

    A number with more digits works the same way. For n = 987654, n % 100 is 54, and the answer is 5 + 4 = 9.

    Complexity

    Time and space are O(1). The method does a fixed number of arithmetic operations, whether n has 2 digits or 18.

    Edge cases and common mistakes

    • Exactly two digits. n = 10 gives 1, and n = 99 gives 18.
    • Zeros at the end. n = 100 has last two digits 00, so the answer is 0. Here n % 100 is 0, and both digits come out as 0.
    • A zero in the tens place. n = 205 gives n % 100 = 5, so the tens digit is 5 / 10 = 0 and the sum is 5. The formula handles this without special cases.
    • Dividing n by 10 instead of lastTwoDigits. 2379 / 10 is 237, not a digit.
    • Using the wrong operator. / gives the quotient and % the remainder. Swapping them gives the wrong digits.

    Testing

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