ProgrammingHardTCS NQT · Accenture

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 count = 0, n, s, d
for (each x from 100 to 999)
    n = x
    s = 0
    while (n > 0)
        d = n mod 10
        s = s + d * d * d
        n = n / 10
    end while
    if (s == x)
        count = count + 1
    end if
end for
Print count
  1. A. 3
  2. B. 5
  3. C. 4
  4. D. 1

Correct answer: C. 4

The loop counts 3-digit numbers equal to the sum of the cubes of their digits (Armstrong numbers): 153, 370, 371 and 407. Output 4.

More Pseudocode and Code Tracing questions

Exams that ask this topic