π Day 35/150 – Count Digits in a Number in Python
Counting digits means finding how many digits are present in a number.
Examples:
12345 → 5 digits
900 → 3 digits
0 → 1 digit
Let’s explore different ways to count digits in Python π
πΉ Method 1 – Using while Loop
print("Digits:", count)
digit at a time using integer division.
πΉ Method 2 – Taking User Input
n = int(input("Enter a number: ")) count = 0 temp = abs(n) while temp > 0: temp //= 10 count += 1 print("Digits:", count)✅ Works with negative numbers too.
πΉ Method 3 – Using String Method
n = 12345 count = len(str(abs(n))) print("Digits:", count)
✅ Easiest and most beginner-friendly method.
πΉ Method 4 – Using Recursion
✅ Great for learning recursive logic.π― Output
Digits: 5
.png)


