swerWhat will be the output of the following Python code?def demo(p,q): if(p == 0): return q else: return demo(p-1,p+q)print(demo(4,5))
Question
swerWhat will be the output of the following Python code?def demo(p,q): if(p == 0): return q else: return demo(p-1,p+q)print(demo(4,5))
Solution
The given Python code defines a recursive function demo(p, q). This function checks if p is equal to 0. If p is 0, it returns q. Otherwise, it calls itself with parameters p-1 and p+q.
Let's break down the execution of demo(4,5):
demo(4,5)is called. Sincepis not 0, it callsdemo(3, 4+5)which isdemo(3,9).demo(3,9)is called. Sincepis not 0, it callsdemo(2, 3+9)which isdemo(2,12).demo(2,12)is called. Sincepis not 0, it callsdemo(1, 2+12)which isdemo(1,14).demo(1,14)is called. Sincepis not 0, it callsdemo(0, 1+14)which isdemo(0,15).demo(0,15)is called. Sincepis 0, it returnsqwhich is 15.
So, the output of the Python code will be 15.
Similar Questions
Select the correct answerWhat will be the output of the following Python code?def demo(p,q): if(p == 0): return q else: return demo(p-1,p+q)print(demo(4,5))Options13Infinite loop155
correct answerWhat will be the output of the following Python code?def demo(x): if (x > 100): return x - 5 return demo(demo(x+11)); print(demo(45))
what is the output of following python code without running the code on python compiler? def test( i, j ): if(j==1): return i else: return test( i+1, j-1 )print(test( 4, 6 ))
ct answerWhat will be the output of the following Python code?def p(num): if num == 0: return 0 elif num == 1: return 1 else: return p(num-1)+p(num-2)for k in range(0,4): print(p(k),end=" ")
What will be the output of the following program in Python?print(0 or 1)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.