Created
December 20, 2020 04:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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