Q. We can determine how many digits a positive integer has by repeatedly dividing by 10 (without keeping the remainder) until the number is less than 10, consisting of only 1 digit. We add 1 to this value for each time we divided by 10.

Implement this recursive functionality in Python and test it using a main function that calls this with the values 15, 105, and 15105.

Hint. Remember, in Python 3.x if n is an integer, n/10 will not be an integer.

 

Answer =

 

 



def count(n):
    if n < 10 :
        return 1
    else :
        return 1 + count( n / 10 )

num = float(input("Enter a number :-"))
print("number of digit ",count(num))


Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post