Dremendo Tag Line

Input a number and count digits using a function in Python

Function - Question 1

In this question, we will see how to input a number and count how many digits are there in it in Python programming using a function. To know more about function click on the function lesson.

Q1) Write a program in Python to input a number and count how many digits are there in it using a function.

Program

def countdigit(num):
    dc=0
    while num>0:
        dc=dc+1
        num=num//10
    return dc


# main program
n=int(input('Enter a number '))
print('Total Number of Digits =',countdigit(n))

Output

Enter a number 42863
Total Number of Digits = 5
video-poster