ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer c = 0, j
for (each i from 1 to 10)
    j = i
    while (j <= 10)
        c = c + 1
        j = j + i
    end while
end for
Print c
  1. A. 55
  2. B. 25
  3. C. 10
  4. D. 27

Correct answer: D. 27

For each i, j runs over i, 2i, 3i, ... up to 10, which is floor(10/i) values. Sum: 10 + 5 + 3 + 2 + 2 + 1 + 1 + 1 + 1 + 1 = 27. (This is also the total number of divisors of 1 to 10.)

More Pseudocode and Code Tracing questions

Exams that ask this topic