GATE CS 2025 Set 1 | Question 63

NAT · 2 marks

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)

Answer

46

Explanation

The first loop multiplies t by 10 until it exceeds n, ending at \(t = 100000\); then t /= 10 gives \(t = 10000\), the place value of the leading digit. The second loop reads the digits of 14362 from left to right: 1, 4, 3, 6, 2.

turn starts at 0 and flips after every digit, so only the 2nd and 4th digits are appended: newnum becomes 4 and then \(10 \times 4 + 6 = 46\). The digits 1, 3 and 2 are skipped.

Answer: 46