Skip to content

Commit 0904610

Browse files
vicky1999cclauss
andauthored
create sum_of_digits.py (TheAlgorithms#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>
1 parent 35319a2 commit 0904610

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

maths/sum_of_digits.py

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

0 commit comments

Comments
 (0)