Skip to main content

GATE CSE 2025Programming in C › Recursion & Program TracingVerified

GATE CSE 2025 PYQ: Consider the following C program: #include <stdio.h> int gate ( int n ) { int d , t , newnum , turn ; newnum…

Consider the following C program:

  1. #include <stdio.h>
  2. int gate (int n) {
  3. int d, t, newnum, turn;
  4. newnum = turn = 0; t=1;
  5. while (n>=t) t *= 10;
  6. t /=10;
  7. while (t>0) {
  8. d = n/t;
  9. n = n%t;
  10. t /= 10;
  11. if (turn) newnum = 10*newnum + d;
  12. turn = (turn + 1) % 2;
  13. }
  14. return newnum;
  15. }
  16. int main () {
  17. printf ("%d", gate(14362));
  18. return 0;
  19. }

The value printed by the given CC program is ________. (Answer in integer)

    Answer

    46

    Approach

    Trace the C function gate() by first determining the largest power of 10 not exceeding n, then extracting digits from left to right. The variable turn alternates 0,1,0,1,... and controls whether the current digit is appended to newnum.

    The program keeps digits at alternating positions, starting by skipping the first digit. For 14362, it keeps 4 and 6, forming 46.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.