LearnFrenzy
Sign in Take a free test

Recursion · Predict the Output · Question 8

Sum of digits recursively Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following Python program?

main.py
def ds(n):
    return n if n < 10 else n % 10 + ds(n // 10)

print(ds(9875), ds(7), ds(ds(9875)))
  • A 29 7 2
  • B 9875 7 29
  • C 28 7 10
  • D 29 7 11 Correct
Correct Answer: Option D

9 + 8 + 7 + 5 = 29, and the digits of 29 sum to 11.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links