Recursion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Recursion

How to get this answer? int go(int x) { if (x<1) return x; else return x + go(x-1); } What is go(6)? //answer 6

10th Mar 2023, 6:05 PM
Sara
Sara - avatar
2 Answers
+ 4
recursive functions can be treated similar to loops in above case the function is repeatedly calling itself till the value of i is >=1 therefore the function is called i times, so, we can simply explain it as: 6+go(6-1) 6+5+go(5-1) ... 6+5+4+3+2+1 = 21
10th Mar 2023, 6:11 PM
I am offline
I am offline - avatar
0
Sara Yamaguchi Please attach the code with programming language tag
10th Mar 2023, 6:07 PM
Sakshi
Sakshi - avatar