GateAI
Programming in C

GATE CSE 2017 Set 2 · Q37

MCQ2 marksMedium

Time

Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variables x, y, q and r are all unsigned int.while (r >= y) { r = r - y; q = q + 1; }Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r)?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) (q == 0) && (r == x) && (y > 0)

Programming in C

The loop computes integer division via repeated subtraction where qq is the quotient and rr is the remainder.

  1. Loop Invariant: The relation x=y⋅q+rx = y \cdot q + r must hold before the loop starts and after every iteration:

    • In each iteration, rr becomes r−yr - y and qq becomes q+1q + 1.

    • The new value is y⋅(q+1)+(r−y)=y⋅q+y+r−y=y⋅q+ry \cdot (q + 1) + (r - y) = y \cdot q + y + r - y = y \cdot q + r.

    • To satisfy this invariant initially, setting q=0q = 0 and r=xr = x gives y⋅0+x=xy \cdot 0 + x = x, which is identically true.

  2. Termination: For the loop while (r >= y) to terminate when dealing with unsigned integers:

    • If y=0y = 0, r≥0r \ge 0 is always true, leading to an infinite loop.

    • Therefore, we must have y>0y > 0.

Combining both requirements gives the precondition: (q == 0) && (r == x) && (y > 0).

Hence, option C is correct.

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 Q37 of the GATE CSE 2017 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. 19 of them are 2-mark MCQs like this one, and GATE CSE 2017 Set 2 has 6.

How GATE tests Programming in C

More from GATE CSE 2017 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2017 Set 2 · Q37 · MCQ

Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variables x , y , q and r are all unsigned int . while (r >= y) { r = r - y; q = q + 1; } Which of the following conditions on the variables x , y , q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r) ?

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.