StreakPeaked· Practice

ExamsGATETechnical

What will be the output of the following C program? void count(int n){ static int d=1; printf("%d ", n); printf("%d ", d); d++; if(n>1) count(n-1); printf("%d ", d); } void main(){ count(3); }

  1. 3 1 2 2 1 3 4 4
  2. 3 1 2 1 1 1 2 2
  3. 3 1 2 2 1 3 4
  4. 3 1 2 1 1 1 2

Correct answer: 3 1 2 2 1 3 4 4

Solution

The correct output is generated by the recursive function, which prints the current value of 'n', the static variable 'd', and increments 'd' with each call. As the recursion unwinds, it prints 'd' again, resulting in the sequence observed in the correct option.

Related GATE Technical questions

⚔️ Practice GATE Technical free + battle 1v1 →