Skip to content

Commit af3842f

Browse files
committed
Add new file
1 parent 49864d9 commit af3842f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

digits.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#Add the digits of a given integer
2+
3+
def digit_sum(x):
4+
digits = str(x) #Convert to string for iteration
5+
total = 0
6+
for i in digits:
7+
total += int(i) #Convert each digit individually back to integer to store and add them
8+
return total
9+
10+
#Other possible Solution
11+
#num = map(int, str(x))
12+
#print sum(num)
13+
14+
#First Idea
15+
# for i in num:
16+
# digits = num.split()
17+
# return sum(digits)
18+
19+
print digit_sum(434)

0 commit comments

Comments
 (0)