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 = 987654321, c = 0, s
while (n >= 10)
    s = 0
    while (n > 0)
        s = s + n mod 10
        n = n / 10
    end while
    n = s
    c = c + 1
end while
Print n, c
  1. A. 45 1
  2. B. 9 1
  3. C. 9 2
  4. D. 0 2

Correct answer: C. 9 2

First pass: digit sum 45; second pass: 4 + 5 = 9, which is below 10. So the digital root 9 is reached after 2 passes: 9 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic