0
How can I made a sum like this 18 = 1 + 8 = 9?
I accept suggestion in all languages expecially ruby and python
2 Answers
+ 3
string_number = str(18)
sum = 0
for ch in string_number:
number = int(ch)
sum += number
print sum
+ 1
both in Python
#1:
number = 18
sum = 0
for char in str(number):
sum += int(char)
print(sum)
#2:
number = 18
sum = 0
while number > 0:
sum += number % 10
number //= 10
print(sum)