GATE CSE 2017 Session 2 Q47: Precondition for division by repeated subtraction loop
- Programming and Data Structures
- 2 marks
- MCQ
- −2/3 if wrong
Syllabus topic: Programming in C
Question
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)?
Try it: multiple choice: exactly one correct option
Show answer and explanation
Answer: C (official GATE 2017 final answer key)
Concept used: Loop invariant reasoning: x == y*q + r is preserved by each iteration, so it must hold initially, and y > 0 is needed for termination.
First step: Check that each iteration keeps y*q + r unchanged, then see which option makes x == y*q + r true before the loop and guarantees r decreases.
Full step-by-step solution
Loading…
Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.
More questions on Programming in C
Every PYQ on this topic →Share this question
Source: official GATE 2017 Session 2 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15