Skip to content

Instantly share code, notes, and snippets.

@ayubmetah
Created December 20, 2020 04:53
Show Gist options
  • Save ayubmetah/41ee0c90be8fa7c44f81ec075eb50939 to your computer and use it in GitHub Desktop.
Save ayubmetah/41ee0c90be8fa7c44f81ec075eb50939 to your computer and use it in GitHub Desktop.
Complete the function digits(n) that returns how many digits the number has. For example: 25 has 2 digits and 144 has 3 digits. Tip: you can figure out the digits of a number by dividing it by 10 once per digit until there are no digits left.
def digits(n):
count = 0
if n == 0:
return 1
while (n > 0):
count += 1
n = n // 10
return count
print(digits(25)) # Should print 2
print(digits(144)) # Should print 3
print(digits(1000)) # Should print 4
print(digits(0)) # Should print 1
@Hiduser2
Copy link

def digits(n):
count = 0
if n == 0:
return 1
while (n > 0):
count += 1
n = n // 10
return count

print(digits(25)) # Should print 2
print(digits(144)) # Should print 3
print(digits(1000)) # Should print 4
print(digits(0)) # Should print 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment