Consider the following C program: #include <stdio.h> int gate (int n) { int d,…

2025

Consider the following C program:

#include <stdio.h>

int gate (int n) {

int d, t, newnum, turn;

newnum = turn = 0; t=1;

while (n>=t) t *= 10;

t /=10;

while (t>0) {

d = n/t;

n = n%t;

t /= 10;

if (turn) newnum = 10*newnum + d;

turn = (turn + 1) % 2;

}

return newnum;

}

int main () {

printf ("%d", gate(14362));

return 0;

}

The value printed by the given C program is _______ . (Answer in integer)

Attempted by 153 students.

Show answer & explanation

Correct answer: 46

Key idea: the function processes digits from the most significant to the least significant and appends every second digit to newnum, starting with the second digit from the left.

  1. Initial setup: n = 14362, newnum = 0, turn = 0. The highest power of 10 t is set to 10000.

  2. Iteration 1 (t = 10000): digit d = 14362 / 10000 = 1. Remaining n becomes 14362 % 10000 = 4362. turn = 0 so this digit is not appended. newnum = 0. turn becomes 1.

  3. Iteration 2 (t = 1000): digit d = 4362 / 1000 = 4. Remaining n becomes 4362 % 1000 = 362. turn = 1 so append this digit: newnum = 0*10 + 4 = 4. turn becomes 0.

  4. Iteration 3 (t = 100): digit d = 362 / 100 = 3. Remaining n becomes 362 % 100 = 62. turn = 0 so do not append. newnum stays 4. turn becomes 1.

  5. Iteration 4 (t = 10): digit d = 62 / 10 = 6. Remaining n becomes 62 % 10 = 2. turn = 1 so append this digit: newnum = 4*10 + 6 = 46. turn becomes 0.

  6. Iteration 5 (t = 1): digit d = 2 / 1 = 2. Remaining n becomes 0. turn = 0 so do not append. Final newnum remains 46.

Answer: 46

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir