ProgrammingVery hardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer a[51] = all zeros (indices 0 to 50)
For i = 1 to 50
    For j = i to 50 step i
        a[j] = 1 - a[j]
    End For
End For
Integer c = 0
For k = 1 to 50
    c = c + a[k]
End For
Print c
  1. A. 7
  2. B. 10
  3. C. 43
  4. D. 25

Correct answer: A. 7

Cell k is toggled once for every divisor of k, so it ends at 1 exactly when k has an odd number of divisors, i.e. when k is a perfect square. Squares up to 50: 1, 4, 9, 16, 25, 36, 49, which makes 7.

More Pseudocode and Code Tracing questions

Exams that ask this topic