• edited by
12,011 views
21 21 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);
  }
}

What does f(173) print?

  1. $010110101$
  2. $010101101$
  3. $10110101$
  4. $10101101$

5 Answers

Best answer
28 28 votes

Answer: D

The function prints the binary equivalent of the number $n$.

Binary equivalent of $173$ is $10101101$.

• edited by
13 13 votes

OUTPUT IS

10101101

i.e Option  D
since recusive funtion calls will be 

1st function call 173/2=86(int part only)


2nd function call 86/2 =43

3rd function call 43/2=21(int part only)

4th function call 21/2=10(int part only)

5th function call 10/2=5

6th function call 5/2=2(integer part only)

7th function call 2/2=1

now in 7th function call condition if(n<=1) will become true 
so  n will be printed( i.e 1 will be printed )

now while returning every function will execute its remaining part of code ie
 

 printf ("%d", n%2);

So 6th function will print 2 mod 2 =0

5th  function will print 5 mod 2=1

4th function will print 10 mod 2 =0

3rd function will print 21 mod 2 =1

2nd function will print 43 mod 2 =1

1st function will print 86 mod 2 =0

the main f function call will print 173 mod 2 =1

10 10 votes

Here  before calling f(n/2) we need to push the printf statement into stack.

and we know stack follows LIFO order so Last in First print here.

Hence D is Ans.

0 0 votes

above function is same as 

 

void f (int n) {

        if (n/2) {

        f(n/2);

       }

       printf ("%d", n%2);

}

and print the same output.

 

Answer : D

Answer:
Position:
Show:

Related questions

23 23 votes
3 answers 3 answers
12.8k
12.8k views
Ishrat Jahan asked Oct 29, 2014
12,775 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); } }Which of the following im...
6 6 votes
1 answers 1 answer
4.1k
4.1k views
go_editor asked Jun 13, 2016
4,111 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$
28 28 votes
2 answers 2 answers
9.2k
9.2k views
go_editor asked Apr 21, 2016
9,229 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.2k
17.2k views
go_editor asked Sep 30, 2014
17,221 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...