DEV Community

Vinoth
Vinoth

Posted on Edited on

Count of Digits program!

Count of Digits:

Count of digits means finding the total number of digits present in a given number.

no = 7516
count=0
no = no//10 #no 751
count+=1 #count=count+1
no = no//10 #no 75
count+=1 #count=count+1
no = no//10 #no 7
count+=1 #count=count+1
no = no//10 #no 0
count+=1 #count=count+1

output : 4

no =7516
count_of_digits =0
while no >0:
     no=no//10
     count_of_digits+=1
else:
    print("count_of_digits:",count_of_digits)

Enter fullscreen mode Exit fullscreen mode

Count of Digits Using a Python Function (def)

def count_of_digits(no):
    count = 0
    while no > 0:
        no = no // 10
        count += 1

    return count


number = 7516
result = count_of_digits (number)
print("Count of digits:", result)

Enter fullscreen mode Exit fullscreen mode

output : 4

Top comments (0)