Class - 12 CBSE Computer Science Important Output Questions 2025
Predict the output of the following code:
def express (x, n) :
if n == 0:
return 1
elif n % 2 == 0:
return express (x*x, n/2)
else:
return x * express (x, n-1)
express (2, 5)
Python Functions
2 Likes
Answer
The code will not print anything because it doesn't include a print statement. Instead, it returns a value without displaying it.
Explanation
The code defines a recursive function express(x, n)
that takes two arguments x, n
. Inside the function, it checks for three conditions: if n is 0, it returns 1; if n is even (checked using the modulo operator), it recursively calls itself with x*x and n/2; and if n is odd, it recursively calls itself with x and n-1, then multiplies the result by x. When express(2, 5)
is called, it eventually evaluates to 32 but does not print or display anything.
Answered By
1 Like