Sum of Digits Recursive
Python
Medium
2 views
Problem Description
Input has one integer n is provided (non-negative). Write recursive function sumdig(n). Output result.
Input Format
One integer n.
Output Format
One integer sum of digits.
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
n=int(s)
def sumdig(x):
if x<10:
return x
return x%10 + sumdig(x//10)
sys.stdout.write(str(sumdig(n)))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!