GATE | GATE CS 2019 | Question 35
Consider the following C program:
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?
(A) It will print the binary representation of n in the reverse order and terminate.
(B) It will print the binary representation of n but will not terminate
(C) It will not print anything and will not terminate.
(D) It will print the binary representation of n and terminate.
Answer: (C)
Explanation: Since n is the integer, so it 1/2 = 0.5 = 0 will return because of integer.
0/2 = 0 will cause infinite loop because there is no terminating condition for 0.
So, option (C) is correct.
Note:
It will print the binary representation of n and terminate, only if condition “if (n <= 0)".
#include <stdio.h> void convert(int n) { if(n <= 0) printf("%d", n); else { convert(n / 2); printf("%d", n%2); }; } int main() { convert (16); } |
Recommended Posts:
- GATE | GATE CS 1999 | Question 36
- GATE | GATE CS Mock 2018 | Question 39
- Top 5 Topics for Each Section of GATE CS Syllabus
- How to prepare in Last 10 days to score high in GATE?
- Aptitude | GATE IT 2006 | Question 7
- Aptitude | GATE IT 2006 | Question 8
- GATE | GATE CS 2013 | Question 1
- GATE | GATE CS 2013 | Question 3
- GATE | GATE CS 2013 | Question 4
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 6
- GATE | GATE CS 2013 | Question 7
- GATE | GATE CS 2013 | Question 8
- GATE | GATE CS 2013 | Question 65
- GATE | GATE CS 2013 | Question 65



