KnowledgeBoat Logo

Computer Science

What is the output of the following snippet?

def fun(n):
    if (n > 100):
        return n - 5
    return fun (fun (n+11) )
print(fun(45))
  1. 50
  2. 100
  3. 74
  4. Infinite loop

Python Functions

1 Like

Answer

100

Reason — Initially, the function fun is called with the argument 45. Since 45 is less than 100, the else block is executed, and fun is called recursively with the argument fun(n+11). This leads to another recursive call fun(56), as 45 + 11 = 56. Again, since 56 is less than 100, the else block is executed, and another recursive call fun(n+11) is made with n = 67. This process continues until n exceeds 100, at which point the base case is triggered.

Answered By

3 Likes


Related Questions