ProgrammingModerateTCS NQT · Wipro

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Integer n = 19, steps = 0, s, d
while (n != 1 AND n != 4)
    s = 0
    while (n > 0)
        d = n mod 10
        s = s + d * d
        n = n / 10
    end while
    n = s
    steps = steps + 1
end while
Print n, steps
  1. A. 4 4
  2. B. 1 4
  3. C. 1 3
  4. D. 1 5

Correct answer: B. 1 4

Sum of squares of digits: 19 → 82 → 68 → 100 → 1. It reaches 1 (a 'happy' number) after 4 steps: 1 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic