GATE 2019 CS – Question 36
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?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (D) It will not print anything and will not terminate
Explanation
For $n\ge0$ the function calls itself with $n/2$, which tends to 0 but never becomes negative. So the recursion never reaches the base case $n<0$, never prints anything and does not terminate (it ends in a stack overflow).