mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
19 lines
318 B
Python
19 lines
318 B
Python
|
def sum_of_digits(n: int) -> int:
|
||
|
"""
|
||
|
Find the sum of digits of a number.
|
||
|
|
||
|
>>> sum_of_digits(12345)
|
||
|
15
|
||
|
>>> sum_of_digits(123)
|
||
|
6
|
||
|
"""
|
||
|
res = 0
|
||
|
while n > 0:
|
||
|
res += n % 10
|
||
|
n = n // 10
|
||
|
return res
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
print(sum_of_digits(12345)) # ===> 15
|