GATE CS 2025 Set 1 | Question 63

Programming & Data Structures › Basics & Control Structures · NAT · 2 marks · Answer: 46

  1. Question 63Programming & Data Structuresnat2 marks
    Consider the 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

    Solution

    Answer: 46 The function reads digits of from most significant to least, and keeps a digit only on alternate turns, starting by skipping the first ( begins at 0, so digit 1 is skipped, digit 2 kept, ...). Digits of : .
    turn=0: d=1  skipped          turn->1
    turn=1: d=4  newnum = 4       turn->0
    turn=0: d=3  skipped          turn->1
    turn=1: d=6  newnum = 46      turn->0
    turn=0: d=2  skipped
    So it keeps the 2nd and 4th digits → .

More GATE PYQ on Basics & Control Structures

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2025 Set 1 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →