Digit Count
Programming Basics · Python
TASK
Problem
Given a non-negative integer up to 999999, print its digit count. Zero has one digit.
EXAMPLE
Example
10500
5
LIMITS
Constraints
n ≥ 0
LEARN
Theory for this problem
+
`input()` reads data entered by the user. The result of `input()` is initially a string. `int()` converts a numeric string into an integer. Comparison operators `==`, `!=`, `<`, `>`, `<=`, `>=` compare values and produce `True` or `False`. `if` checks a condition and runs its block when the condition is true. `else` runs when the corresponding `if` condition is false. `print()` displays the program result. Print only what the problem statement requires.
**Connection to this task.** An `if` statement chooses a program branch from a Boolean condition. Comparisons produce `True` or `False`, and `elif` lets you test several mutually exclusive cases in order. The expression `1` shows how this idea is applied to the task data.
NEED HELP?
Hints
+
['Without loops, compare against 10, 100, 1000, etc.']
ANSWER
Solution
+
n = int(input())
if n < 10:
print(1)
elif n < 100:
print(2)
elif n < 1000:
print(3)
elif n < 10000:
print(4)
elif n < 100000:
print(5)
else:
print(6)