ProgrammingHardTCS NQT Advanced · Infosys SP

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 3524, steps = 0
while (n != 6174)
    Integer big = digits of n sorted in decreasing order (4 digits, zeros kept)
    Integer small = digits of n sorted in increasing order
    n = big - small
    steps = steps + 1
end while
Print steps
  1. A. 1
  2. B. 3
  3. C. 2
  4. D. 7

Correct answer: B. 3

Kaprekar's routine: 5432 − 2345 = 3087; 8730 − 0378 = 8352; 8532 − 2358 = 6174. Three steps.

More Pseudocode and Code Tracing questions

Exams that ask this topic