Start learning free

LESSON 216 OF 291 · WHILE LOOPS - CHALLENGES

Count the Digits of a Number in Kotlin

Practice Read the full explanation

Given a non-negative integer n, return how many digits it has.

The digits are the figures from 0 to 9 that n is written with. Dividing a number by 10 without the remainder removes its last digit (542 becomes 54), so you can count how many times that happens until the number is 0. The number 0 itself has one digit.

 

Example 1:

Input: n = 542
Output: 3
Explanation: 542 is written with three digits: 5, 4 and 2.

Example 2:

Input: n = 100
Output: 3
Explanation: The zeros are digits too: 1, 0 and 0.

Example 3:

Input: n = 0
Output: 1
Explanation: 0 is written with one digit.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 7
    Ctrl + Enter to check

    KOTLIN · WHILE LOOPS - CHALLENGES

    No Of Digits in Kotlin, explained

    Problem overview

    The function noOfDigits receives a non-negative integer n and returns how many digits it is written with. 542 has 3, 100 has 3 (the zeros are digits too), and 0 has 1.

    Approach

    Integer division by 10 removes the last digit: 542 / 10 is 54, then 5, then 0. The number of divisions needed to reach 0 is the number of digits, so a loop can count them.

    Printing the values after each pass for 542 shows it:

    Passnum aftercount after
    1541
    252
    303

    The loop has one blind spot. It runs only while the number is above 0, and 0 is already 0, so it would count no digits at all for it. But 0 is written with one digit, so that case gets its own answer first.

    Step by step

    1. Handle zero. If n == 0, return 1 right away.
    2. Copy n into num with var, because parameters are read-only and the loop must change the value.
    3. Start count at 0. No digit has been removed yet.
    4. Loop while num > 0. Every pass removes one digit.
    5. Count it with count++.
    6. Drop the last digit by storing num / 10 back in num.
    7. Return count.

    The solution in Kotlin

    class Solution {
        fun noOfDigits(n: Int): Int {
            if (n == 0) {
                return 1
            }
            var num = n
            var count = 0
            while (num > 0) {
                count++
                num = num / 10
            }
            return count
        }
    }
    

    Some details worth knowing:

    • count++ adds 1 to a var. It is shorthand for count += 1.
    • Integer division. With two Int values, / drops the remainder, so 5 / 10 is 0 and the loop ends after the last digit.
    • The largest input. 1000000000 fits in an Int and has 10 digits, which the function returns.

    Another way: through a string

    Kotlin can also turn the number into text and measure it. 1000000000.toString().length is 10, which agrees with the loop. That is shorter, but it builds a string only to throw it away, and it hides the idea this lesson practises: each division by 10 removes one digit. The loop version also carries over to bases other than 10, where you divide by the base instead.

    Why zero needs its own case

    A loop that starts with while (num > 0) asks "is there a digit to remove?" For every positive number the answer is yes at least once, but for 0 it is no from the start, even though the number is written with a digit. Checking n == 0 first keeps the loop simple. Moving the check into the loop, for example with a do { ... } while form, also works, but the early return is easier to read.

    Complexity

    Time is O(d) for a number with d digits: one pass per digit. Space is O(1).

    Edge cases and common mistakes

    • Zero. Without the early return the function would give 0 instead of 1.
    • Numbers ending in zeros. 100 goes 100, 10, 1, 0 and counts 3.
    • Forgetting to shrink num. The loop would never end.
    • Returning inside the loop. Return after it, once every digit is counted.

    Testing

    • 542 gives 3.
    • 100 gives 3.
    • 0 gives 1.
    • 1000000000 gives 10.
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free