GateAI
Programming in C

GATE CSE 2021 Set 2 · Q23

NAT1 markEasy

Time

Consider the following ANSI C function:int SomeFunction(int x, int y) { if ((x == 1) || (y == 1)) return 1; if (x == y) return x; if (x > y) return SomeFunction(x - y, y); if (y > x) return SomeFunction(x, y - x); }The value returned by SomeFunction(15, 255) is ________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 15

Programming in C

The given C function implements the Euclidean algorithm for computing the greatest common divisor gcd⁡(x,y)\gcd(x, y) using repeated subtraction:

  • If either operand is 11, it returns 11.

  • If both operands are equal, it returns that value.

  • Otherwise, it subtracts the smaller number from the larger number recursively.

Since 255=17×15255 = 17 \times 15, subtracting 1515 repeatedly from 255255 eventually yields (15,15)(15, 15). When x=15x = 15 and y=15y = 15, the condition if (x == y) evaluates to true and returns 15.

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 Q23 of the GATE CSE 2021 Set 2 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. 16 of them are 1-mark NAT questions like this one, and GATE CSE 2021 Set 2 has 3.

How GATE tests Programming in C

More from GATE CSE 2021 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2021 Set 2 · Q23 · NAT

Consider the following ANSI C function: int SomeFunction(int x, int y) { if ((x == 1) || (y == 1)) return 1; if (x == y) return x; if (x > y) return SomeFunction(x - y, y); if (y > x) return SomeFunction(x, y - x); } The value returned by SomeFunction(15, 255) is …

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.