Exams › GATE › Technical
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 ________ (An answer in integer)
Correct answer: The value printed by the given C program is ________ (An answer in integer)
Solution
The function 'gate' processes the digits of the input number by alternating between skipping and including digits, effectively reversing the order of every second digit starting from the least significant one. For the input 14362, it retains the digits 4, 3, and 2, resulting in the output 42.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →