I have this function in Python that helps me to find the sum of a number's digits:
def sum01(n):
if n < 9 and n > 0:
return n
elif n > 9:
s = sum01((n%10)+(n//10))
return s
else:
return "ERROR, only natural numbers (N)!"
print(sum01(22224))
The results this function give are:
3
However, the results I wish to see are:
12
I want to get this answer using a recursive function without the use of while
or for
loops and no lists, however, I have been failing in doing so.
Thanks!
I couldn't add the solution I have been searching for as an answer so here it is in the question.
`def sum01(n):
x = 0
if n < 9 and n > 0:
return n
elif n > 9:
x = (n%10) + x
s = sum01(n//10)
return s + x
else:
return "ERROR, only natural numbers (N)!"`