Skip to content

Commit 39c74e6

Browse files
committed
sum of digits
1 parent 22d3a22 commit 39c74e6

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

13. logic/medium/sum_of_digits.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
# Output: An integer representing the sum of the digits of n
44

55

6-
6+
def sum_of_digits(num):
7+
sum = 0 # Initialize sum to 0
8+
str_num = str(num) # Convert the integer to a string to iterate over each digit
9+
for digit in str_num: # Iterate over each character in the string
10+
sum += int(digit) # Convert the character back to an integer and add it to the sum
11+
return sum # return total_sum
12+
13+
print(sum_of_digits(43)) # Output 7
714

815

0 commit comments

Comments
 (0)