Skip to content

Commit e402489

Browse files
vicky1999cclauss
authored andcommitted
Create number_of_digits.py (TheAlgorithms#1975)
* Create number_of_digits.py A python program to find the number of digits in a number. * Update number_of_digits.py * Update number_of_digits.py * Add TheAlgorithms#1976 to get Travis CI to pass TheAlgorithms#1976 * Add type hints as discussed in CONTRIBUTING.md Co-authored-by: Christian Clauss <[email protected]>
1 parent a18b921 commit e402489

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

maths/number_of_digits.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def num_digits(n: int) -> int:
2+
"""
3+
Find the number of digits in a number.
4+
5+
>>> num_digits(12345)
6+
5
7+
>>> num_digits(123)
8+
3
9+
"""
10+
digits = 0
11+
while n > 0:
12+
n = n // 10
13+
digits += 1
14+
return digits
15+
16+
17+
if __name__ == "__main__":
18+
print(num_digits(12345)) # ===> 5

0 commit comments

Comments
 (0)