Start learning free

LESSON 215 OF 289 · WHILE LOOPS - CHALLENGES

Number of Digits in C#: Counting Divisions by 10

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 6
    Ctrl + Enter to check

    C# · WHILE LOOPS - CHALLENGES

    No Of Digits in C#, explained

    Problem overview

    NoOfDigits receives a non-negative int n and returns how many digits it has. 542 has 3, 100 has 3 (zeros are digits too) and 0 has 1.

    Approach

    Dividing an int by 10 with / drops the last digit, because integer division truncates: 542 / 10 is 54. So the number of digits is the number of times you can divide by 10 before reaching 0. For 542 that goes 542, 54, 5, 0: three divisions.

    The number 0 is the one case that breaks this. It has one digit, but it is already 0, so a loop that runs while n > 0 would run zero times and answer 0. The lesson handles it first, with its own if.

    Step by step

    1. If n is 0, return 1. This is the special case.
    2. Create count as an int set to 0. It counts the digits removed so far.
    3. Write while (n > 0). Each pass removes one digit.
    4. Add 1 to count with ++. One pass is one digit.
    5. Divide n by 10 and store the result back in n.
    6. Return count after the loop.

    For n = 542:

    Passcountn after dividing
    1154
    225
    330

    The solution in C#

    class Solution {
        public int NoOfDigits(int n) {
            if (n == 0) {
                return 1;
            }
            int count = 0;
            while (n > 0) {
                count++;
                n = n / 10;
            }
            return count;
        }
    }

    The early return ends the method at once, so the loop code is only reached for numbers above 0. The loop never reads the digits themselves. It only counts how many times it can divide, so a 0 inside the number, as in 100, is counted like any other digit.

    An alternative: do-while

    C# has a loop that runs its body once before it tests the condition: do { ... } while (condition);. With it, the special case for 0 is no longer needed, because one pass always happens and counts the single digit of 0:

    int count = 0;
    do {
        count++;
        n = n / 10;
    } while (n > 0);

    This gives 1 for 0, 3 for 542 and 3 for 100. The lesson uses the early return and a plain while, which is the form you will meet in most code, but both are correct.

    Why dividing by 10 counts digits

    In base 10, every digit is worth ten times the one on its right. Dividing by 10 and dropping the remainder shifts the whole number one place to the right, so the last digit falls off. A number with d digits can be shifted d times before nothing is left. That is why the loop count equals the digit count.

    The loop only needs the size of n, not its digits, so it does not use % at all. Compare it with a digit sum, where n % 10 is needed to read each digit before removing it.

    Edge cases and common mistakes

    • Zero. Without the first if, NoOfDigits(0) would return 0.
    • Trailing zeros. 100 goes 100, 10, 1, 0, which is three passes. The answer is 3.
    • Largest input. 1000000000 has 10 digits, and the loop returns 10. It fits easily in an int.
    • Forgetting the division. If n is never changed, the loop never ends.

    Testing

    The solution returns 3 for 542, 3 for 100, 1 for 0 and 10 for 1000000000.

    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free