Skip to content
Related Articles

Related Articles

Improve Article

GATE | Gate IT 2008 | Question 80

  • Last Updated : 28 Jun, 2021
Geek Week

Consider the code fragment written in C below :




void f (int n)
{
    if (n <= 1)  {
        printf ("%d", n);
    }
    else {
        f (n/2);
        printf ("%d", n%2);
    }
}

Which of the following implementations will produce the same output for f(173) as the above code?
P1




void f (int n)
{
    if (n/2)  {
        f(n/2);
    }
    printf ("%d", n%2);
}

P2




void f (int n)
{
    if (n <=1)  {
        printf ("%d", n);
    }
    else {
        printf ("%d", n%2);
        f (n/2);
    }
}

(A) Both P1 and P2
(B) P2 only
(C) P1 only
(D) Neither P1 nor P2


Answer: (C)

Explanation:  

Here, basically the function f prints the binary representation of the number.
function f1 also prints the binary representation of the number
function f2 prints the binary representation but in reverse order.
Output of f  is:- 10101101
Output of f1 is:- 10101101
Output of f2 is:- 10110101



So, the answer is option (C) which is P1 only.

This solution is contributed  by Anil Saikrishna Devarasetty.


Quiz of this Question

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.




My Personal Notes arrow_drop_up
Recommended Articles
Page :