Decode School

Decode School - The School for Problem Solvers
Welcome to My Blog.
Need Assignment / Project Guidance (or) Training in Python?

Count the number of digits in a Number in Python

Write a program to count the number of digits of a given number.

Sample Input 1:

345

Sample Output 1:

3

Sample Input 2:

56

Sample Output 2:

2


Program or Solution

					    
							
n = int(input())
count  = 0
while  n > 0:
    n = n // 10
    count = count + 1
print(count)

Program Explanation

if we divide a number by 10, then last digit of the same number will removed and we get remaining digits as output. Do the same till number becomes 0 and count iteration. So the count is number of digits of the given number .