ProgrammingHardTCS Digital · Infosys pattern

What will be the output of the following pseudocode? (reverse(x) returns x with its decimal digits reversed.)

What will be the output of the following pseudocode? (reverse(x) returns x with its decimal digits reversed.)
Integer n = 87, k = 0
while (n != reverse(n))
    n = n + reverse(n)
    k = k + 1
end while
Print k, n
  1. A. 3 1353
  2. B. 4 1353
  3. C. 4 4884
  4. D. 5 4884

Correct answer: C. 4 4884

87 + 78 = 165; 165 + 561 = 726; 726 + 627 = 1353; 1353 + 3531 = 4884, which is a palindrome. Four additions. Stopping at 1353 assumes it is a palindrome (it is not: reversed it is 3531).

More Pseudocode and Code Tracing questions

Exams that ask this topic