Example HTML page

# A python program to count the number of digits in a number.

Under this post i am going to provide you the explanation of a python program to count the number of digits in a number.

For example if a number is 12345 then your program should print 5, since the number contains 5 digits.

```count=0
num=int(input("Give any number: "))
while(num>0):
num=num//10
count=count+1
print("Total digits are: ",count)

```

Here the ‘count’ is initialized to zero. Take input from the user and create a variable for storing the input by the user. By using a while loop every time the ‘num’ changes to num//10.Let 12 be the ‘num’ here and then 12//10 is 1.Increment the ‘count’ by 1.Repeat the same process until the ‘num’ becomes zero.Finally print the output.

Output

 Give any number: 524                    #Case 1 Total digits are: 3  Give any number: 002                  #Case 2 Total digits are:  1   Give any number: 1.24                 #Case 3 Total digits are:  1   Give any number: 123.456            #Case 4 Total digits are:  3

If you observe the output of case 3 and case 4 as well, the digit before the decimal point is printed and rest of the part is ignored.

We can also count the number of digits in a number by the following way:

```count = 0
num = int(input("Any number: "))
print ("Total digits : ",len(str(abs(num))))

```

Output

 Any number: 12364767 Total digits :  8

‘abs’ is used to get the absolute value of a number. i.e. to handle the case even if there occurs negative value.

Example HTML page