We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 22d3a22 commit 39c74e6Copy full SHA for 39c74e6
13. logic/medium/sum_of_digits.py
@@ -3,6 +3,13 @@
3
# Output: An integer representing the sum of the digits of n
4
5
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
14
15
0 commit comments