mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00:00
Create number_of_digits.py (#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 #1976 to get Travis CI to pass #1976 * Add type hints as discussed in CONTRIBUTING.md Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
1a254465e3
commit
dc720a83d7
18
maths/number_of_digits.py
Normal file
18
maths/number_of_digits.py
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
def num_digits(n: int) -> int:
|
||||||
|
"""
|
||||||
|
Find the number of digits in a number.
|
||||||
|
|
||||||
|
>>> num_digits(12345)
|
||||||
|
5
|
||||||
|
>>> num_digits(123)
|
||||||
|
3
|
||||||
|
"""
|
||||||
|
digits = 0
|
||||||
|
while n > 0:
|
||||||
|
n = n // 10
|
||||||
|
digits += 1
|
||||||
|
return digits
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(num_digits(12345)) # ===> 5
|
Loading…
Reference in New Issue
Block a user