ProgrammingHardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a = 91, b = 35, steps = 0
While a != b
    If a > b Then
        a = a - b
    Else
        b = b - a
    End If
    steps = steps + 1
End While
Print a, steps
  1. A. 7 4
  2. B. 7 3
  3. C. 7 6
  4. D. 7 5

Correct answer: D. 7 5

Subtractive Euclid: (91,35) -> (56,35) -> (21,35) -> (21,14) -> (7,14) -> (7,7), five subtractions, ending at gcd 7. The modulo version of Euclid would need only 4 steps, but this loop subtracts one multiple at a time.

More Pseudocode and Code Tracing questions

Exams that ask this topic