Start learning free

LESSON 53 OF 349 · CALCULATIONS - CHALLENGES

Last Two Digit Sum in Swift

Practice

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

    Last Two Digit Sum in Swift

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