retagged by
16,465 views
23 votes
23 votes

Consider the following C function.

void convert (int n ) {
        if (n<0)
            printf{“%d”, n);
        else {
            convert(n/2);
            printf(“%d”, n%2);
        }
}

Which one of the following will happen when the function convert is called with any positive integer $n$ as argument?

  1. It will print the binary representation of $n$ and terminate
  2. It will print the binary representation of $n$ in the reverse order and terminate
  3. It will print the binary representation of $n$ but will not terminate
  4. It will not print anything and will not terminate
retagged by

8 Answers

1 votes
1 votes
If we keep dividing a no by 2 it will decrease continuously but will not reach neg.

So the (n<0) will never execute and as control never gets past convert() function because it calls convert again and gets inside. So even printf("%d", n%2) will not be executed ever.

The program will neither print anything nor will it ever terminate.
0 votes
0 votes
Let's analyze how will the program will behave if the input is 1.
convert(1)
  convert(1/2) or convert(0):  now this call
       will not enter the base case since 0 < 0
        is false.
        convert (0/2) or convert (0).....
So hypothetically program will not terminate and it also doesn't print anything since it goes into infinite recursion. Best answer is D
0 votes
0 votes
i think if the following c program is run on a machine with infinite memory it will not terminate or if it is run by hand by a human (just a thought experiment).

So it will not print anything and it will not terminate..
Answer:

Related questions

13 votes
13 votes
4 answers
1
Arjun asked Feb 7, 2019
9,567 views
Consider the following C program :#include<stdio.h int jumble(int x, int y){ x = 2*x+y; return x; } int main(){ int x=2, y=5; y=jumble(y,x); x=jumble(y,x); printf("%d \n"...
12 votes
12 votes
3 answers
4