Count Digits (No Strings)
Python
Easy
4 views
Problem Description
Read one integer n. Count number of digits in |n| without converting to string. For n=0 answer is 1.
Input Format
One integer n.
Output Format
One integer digitCount.
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
n=abs(int(s))
if n==0:
sys.stdout.write('1')
else:
c=0
while n>0:
c+=1
n//=10
sys.stdout.write(str(c))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!