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

Recursion-outputQ

int boogie( int x, int y){ if(y==2) return x; else return boogie(x,y-1) + x; } What is returned by boogie(4,6)? //answer:20

10th Mar 2023, 6:20 PM
Sara
Sara - avatar
1 Answer
+ 1
Your boogie(... ) Function is recursive function and u passing two values 4 and 6 inside function body u have written if(y==2) then it will return 2 so everytime your else part will execute until this condition y==2 satisfy in else part return boogie (4, 6-1) + 4 Here again function called so first value not changing but second value 6 is decreasing by 1 on every call so it will be like this return boogie (4, 6-1) + 4 return boogie (4, 5-1) + 4 +4 return boogie (4, 4-1) + 4 +4 +4 return boogie (4, 3-1) + 4 +4 +4 +4 Now if condition will be true and return 4 so overall sum will be 20
10th Mar 2023, 6:27 PM
A S Raghuvanshi
A S Raghuvanshi - avatar