GateAI
Programming in C

GATE CSE 2025 Set 1 · Q63

NAT2 marksMedium

Time

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)

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 46

Programming in C

Let us trace the execution of the function gate(14362) step-by-step:

  1. Initialization:

    • n=14362n = 14362

    • newnum=0\text{newnum} = 0, turn=0\text{turn} = 0, t=1t = 1

  2. Finding the place value tt:

    • The loop while (n >= t) t *= 10; executes for t=1,10,100,1000,10000,100000t = 1, 10, 100, 1000, 10000, 100000.

    • When t=100000t = 100000, the condition 14362≥10000014362 \ge 100000 is false.

    • Then t /= 10; sets t=10000t = 10000.

  3. Extracting digits left-to-right (while (t > 0)):

    • Iteration 1 (t=10000t = 10000):

      • d=14362/10000=1d = 14362 / 10000 = 1

      • n=14362(mod10000)=4362n = 14362 \pmod{10000} = 4362

      • t=10000/10=1000t = 10000 / 10 = 1000

      • turn=0\text{turn} = 0 (false), so newnum=0\text{newnum} = 0

      • turn=(0+1)%2=1\text{turn} = (0 + 1) \% 2 = 1

    • Iteration 2 (t=1000t = 1000):

      • d=4362/1000=4d = 4362 / 1000 = 4

      • n=4362(mod1000)=362n = 4362 \pmod{1000} = 362

      • t=1000/10=100t = 1000 / 10 = 100

      • turn=1\text{turn} = 1 (true), so newnum=10×0+4=4\text{newnum} = 10 \times 0 + 4 = 4

      • turn=(1+1)%2=0\text{turn} = (1 + 1) \% 2 = 0

    • Iteration 3 (t=100t = 100):

      • d=362/100=3d = 362 / 100 = 3

      • n=362(mod100)=62n = 362 \pmod{100} = 62

      • t=100/10=10t = 100 / 10 = 10

      • turn=0\text{turn} = 0 (false), so newnum=4\text{newnum} = 4

      • turn=(0+1)%2=1\text{turn} = (0 + 1) \% 2 = 1

    • Iteration 4 (t=10t = 10):

      • d=62/10=6d = 62 / 10 = 6

      • n=62(mod10)=2n = 62 \pmod{10} = 2

      • t=10/10=1t = 10 / 10 = 1

      • turn=1\text{turn} = 1 (true), so newnum=10×4+6=46\text{newnum} = 10 \times 4 + 6 = 46

      • turn=(1+1)%2=0\text{turn} = (1 + 1) \% 2 = 0

    • Iteration 5 (t=1t = 1):

      • d=2/1=2d = 2 / 1 = 2

      • n=2(mod1)=0n = 2 \pmod{1} = 0

      • t=1/10=0t = 1 / 10 = 0

      • turn=0\text{turn} = 0 (false), so newnum=46\text{newnum} = 46

      • turn=(0+1)%2=1\text{turn} = (0 + 1) \% 2 = 1

  4. Result:

    • The loop terminates as t=0t = 0.

    • The function returns newnum = 46, which is printed by main().

Thus, the printed value is 46.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q63 of the GATE CSE 2025 Set 1 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 17 of them are 2-mark NAT questions like this one, and GATE CSE 2025 Set 1 has 3.

How GATE tests Programming in C

More from GATE CSE 2025 Set 1

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 1 · Q63 · NAT

Consider the following C program: #include 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)

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.