oneby7

Foundations Working with digits and divisors

27. Count the Digits of a Number

Easy

Given an integer n, return how many digits it has when it is written out in the usual way.

A negative number is written with a minus sign in front. The minus sign is not a digit, so −312 has 3 digits, just like 312. Zeros count like any other digit, so 70500 has 5 digits. The number 0 is written as a single digit, so it has 1 digit.

Example 1

Input: n = 70500

Output: 5

Explanation: The digits are 7, 0, 5, 0 and 0. Every zero counts, the ones at the end included.

Example 2

Input: n = -312

Output: 3

Explanation: The digits are 3, 1 and 2. The minus sign in front is not a digit.

Example 3

Input: n = 0

Output: 1

Explanation: 0 is written with one digit.

Check your understanding

Work out the answer for this input before you write any code.

n = -40500

Constraints

  • -9 × 1015 ≤ n ≤ 9 × 1015

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.