GATE | Gate IT 2005 | Question 57
What is the output printed by the following program?
#include<stdio.h>
int f( int n, int k)
{
if (n == 0)
return 0;
else if (n % 2)
return f(n/2, 2*k) + k;
else return f(n/2, 2*k) - k;
}
int main ()
{
printf ( "%d" , f(20, 1));
return 0;
}
|
(A) 5
(B) 8
(C) 9
(D) 20
Answer: (C)
Explanation:
f(20,1) = 9.
f(10,2) - 1 = 9
f(5,4) - 2 = 10
f(2,8) + 4 = 12
f(1,16) - 8 = 8
f(0,32) + 16 = 16
Quiz of this Question
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...