Consider the C program fragment below which is meant to divide x by y using repeated subtraction. 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)?

Consider the C program fragment below which is meant to divide x by y using repeated subtraction. 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)? Correct Answer (q == 0) && (r == x) && (y > 0)

Given condition is x == (y*q + r)

Here, x= result, y= multiplicand, q= quotient, r= remainder

As, the number is divided using repeated subtraction, So quotient must be 0 in that case.

When in above condition q= 0

Then, x = r.

It matches with option 3.

Related Questions

Comment on the following 2 C programs.
#include  //Program 1int main(){ int a; int b; int c;}#include  //Program 2int main(){ int a; { int b; } { int c; }}