162k views
5 votes
Write a function digits() that accepts a non-negative integer argument n and returns the number of digits in it’s decimal representation. For credit, you should implement this using a loop and repeated integer division; you should not use math.log(), math.log10(), or conversion to string

1 Answer

2 votes

Answer:

Step-by-step explanation:

Let do this in python. We will utilize the while loop and keep on dividing by 10 until that number is less than 10. Along the loop we will count the number of looping process. Once the loop ends, we can output that as answer.

def digits(n):

n_digit = 1

while n >= 10:

n_digit += 1

n /= 10

return n_digit

User Jiahut
by
6.9k points