
Problem
Solution
Submissions
Count the Number of Digits in an Integer
Certification: Basic Level
Accuracy: 60.65%
Submissions: 155
Points: 5
Write a Python program that counts the number of digits in a given integer.
Example 1
- Input: 12345
- Output: 5
- Explanation:
- Step 1: Take the input number 12345.
- Step 2: Count the digits: 1, 2, 3, 4, 5.
- Step 3: Return the count 5.
Example 2
- Input: -9876
- Output: 4
- Explanation:
- Step 1: Take the input number -9876.
- Step 2: Ignore the negative sign and work with 9876.
- Step 3: Count the digits: 9, 8, 7, 6.
- Step 4: Return the count 4.
Constraints
- -10^9 ≤ number ≤ 10^9
- For negative numbers, ignore the negative sign
- Time Complexity: O(log n), where n is the input number
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Convert to string: len(str(abs(number)))
- Use logarithm: import math; int(math.log10(abs(number))) + 1 if number != 0 else 1
- Use iterative counting: count = 0; while abs(number) > 0: count += 1; number //= 10
- Use recursion: def count_digits(n): return 1 if abs(n) < 10 else 1 + count_digits(abs(n) // 10)