Start learning free

LESSON 204 OF 505 · WHILE LOOPS - CHALLENGES

Number of Digits in Go

Practice

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

    No Of Digits in Go

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