edited by
14,038 views
36 votes
36 votes

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 terminated in a state satisfying the condition $x==(y*q + r)$?

  1. $(q==r) \ \&\& \ (r==0)$
  2. $(x>0) \ \&\&  \ (r==x) \ \&\& \ (y>0)$
  3. $(q==0) \ \&\& \ (r==x) \ \&\& \ (y >0)$
  4. $(q==0) \ \&\& \ (y>0)$
edited by

6 Answers

0 votes
0 votes
Just substitute the values in the variables.

let r=3, y>0 i.e,1 etc and q=0 and substitute in the each options then which one satisfied that is the answer.
Answer:

Related questions