• edited by
12,929 views
23 23 votes

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 P2
void f (int n)
{ 
    if (n/2)  {
        f(n/2);
    }
    printf ("%d", n%2);
}
void f (int n)
{ 
    if (n <=1)  {
        printf ("%d", n);
    }
    else {
        printf ("%d", n%2);
        f (n/2);
    }
}
  1. Both $P1$ and $P2$
  2. $P2$ only
  3. $P1$ only
  4. Neither $P1$ nor $P2$

3 Answers

Best answer
30 30 votes
Answer: C

The code fragment written in C and P1 prints the binary equivalent of the number n.

P2 prints the binary equivalent of the number n in reverse.
• selected by
36 36 votes

Here, $P1$ and $P2$ will print opposite in direction as shown in diagram.

And given code fragment will print like $P1$ and not like $P2$

Hence, answer will be (C).

• edited by
6 6 votes
ans is 3). As P1 prints 10101101 same as given code in question but P2 prints the output in reverse order.
Answer:
Position:
Show:

Related questions

21 21 votes
5 answers 5 answers
12.1k
12.1k views
Ishrat Jahan asked Oct 29, 2014
12,099 views
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); } }What does f(173) print?$010110101$...
6 6 votes
1 answers 1 answer
4.1k
4.1k views
go_editor asked Jun 13, 2016
4,140 views
What is the value of $F(4)$ using the following procedure:function F(K : integer) integer; begin if (k<3) then F:=k else F:=F(k-1)*F(k-2)+F(k-3) end;$5$$6$$7$$8$
30 30 votes
2 answers 2 answers
9.3k
9.3k views
go_editor asked Apr 21, 2016
9,328 views
Consider the following recursive C function that takes two arguments.unsigned int foo(unsigned int n, unsigned int r) { if (n>0) return ((n%r) + foo(n/r, r)); else return...
43 43 votes
5 answers 5 answers
17.4k
17.4k views
go_editor asked Sep 30, 2014
17,421 views
What is the value printed by the following C program?#include<stdio.h int f(int *a, int n) { if (n <= 0) return 0; else if (*a % 2 == 0) return *a+f(a+1, n-1); else retur...