mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
create sum_of_digits.py (#2065)
* create sum_of_digits.py create sum_of_digits.py to find the sum of digits of a number digit_sum(12345) ---> 15 digit_sum(12345) ---> 10 * Update sum_of_digits.py * Update maths/sum_of_digits.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update maths/sum_of_digits.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update sum_of_digits.py Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
35319a2a2a
commit
0904610a76
18
maths/sum_of_digits.py
Normal file
18
maths/sum_of_digits.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
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
|
Loading…
Reference in New Issue
Block a user