Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following pseudo code, where x and y are positive integers.
    begin
    q: = 0
    r: = x
    while r > y do
       being
        r: = r – y
        q: = q + 1
       end
    end
    The post condition that needs to be satisfied after the program terminates is
    1. {r = qx + y ∧ r < y}
    2. {x = qy + r ∧ r < y}
    3. {y = qx + r ∧ 0 < r < y}
    4. {q + 1 < r – y ∧ y > 0}
Correct Option: B

The loop terminater when r < y. so, r < y is one post condition.
In each iteration q is incremented by 1 and y is subtracted from r. Initialvalue of r is x.

So, loop iterates
x
times and q will be equal to
x
yy

and r = x% y ⇒ x = qy + r
So, (b) is correct answer.



Your comments will be displayed only after manual approval.