What will be the output for the pseudo-code for p=22, q=127 fun(int p,int q)…

2025

What will be the output for the pseudo-code for p=22, q=127

fun(int p,int q)

if (p == 1)

return q;

else if (p%2==0)

return fun (p - 1 , q);

else

return 0;

  1. A.

    9

  2. B.

    6

  3. C.

    5

  4. D.

    None of the above mentioned

Attempted by 48 students.

Show answer & explanation

Correct answer: D

  1. First Call: fun(22, 127)

    • Check if (p == 1) 22 == 1 is False.

    • Check else if (p % 2 == 0) 22 % 2 == 0 is True.

    • Action: It returns fun(22 - 1, 127), which triggers the next call: fun(21, 127).

  2. Second Call: fun(21, 127)

    • Check if (p == 1) 21 == 1 is False.

    • Check else if (p % 2 == 0) 21 % 2 == 0 is False (since 21 is odd).

    • Action: It falls into the final else block and explicitly return 0;.

Because the second recursive call encounters an odd number that is not equal to 1, it immediately hits the else block and terminates the recursion by returning 0.

Final Answer:

The output of the pseudo-code for p = 22, q = 127 is 0.

Explore the full course: Coding For Placement