ProgrammingEasyInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 1234, r = 0
while (n > 0)
    r = r * 10 + (n mod 10)
    n = n / 10        // integer division
end while
Print r
  1. A. 1234
  2. B. 10
  3. C. 432
  4. D. 4321

Correct answer: D. 4321

Each iteration appends the last digit of n to r: 4, 43, 432, 4321. Summing the digits instead would give 10. O(number of digits).

More Pseudocode and Code Tracing questions

Exams that ask this topic