Question DSA-REC-0006
EasyWhat is the output of this code?
def sum_to(n):
return 0 if n == 0 else n + sum_to(n - 1)
print(sum_to(4))
- A.
4
- B.
15
- C.
Show answer and explanation
Answer: C
sum_to(4) = 4 + 3 + 2 + 1 + 0 = 10.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Recursion and Backtracking question
What is the output of this code?
def sum_to(n):
return 0 if n == 0 else n + sum_to(n - 1)
print(sum_to(4))
4
15
Answer: C
sum_to(4) = 4 + 3 + 2 + 1 + 0 = 10.
10
6