GATE | Gate IT 2008 | Question 80

Consider the code fragment written in C below :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


P2



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(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



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.