Menu
CoddyTech

Count Digits

Given a non-negative integer n, return how many digits it has when written in base 10 without leading zeros. Zero is written as a single 0, so it has one digit.

Function

countDigits(n: integer) → integer
ninteger
the non-negative integer to measure
Returnsinteger
the number of decimal digits in n

Constraints

  • 0 ≤ n ≤ 231-1

Examples

Input
n = 4096
Output
4
Explanation
Integer division by 10 takes 4096 to 409, 40 and 4: three digits removed and one left, so the answer is 4.
challenge icon

Follow-up

Can you count the digits without a loop that runs once per digit, for example with a binary search over the powers of ten?

Reset code
def countDigits(n):
    # Write code here

Test #1

Test #2

Test #3

Argument

n = 4096

Expected Output

4