ProgrammingVery hardProduct company OA

What will be the output of the following pseudocode? (/ is integer division.)

What will be the output of the following pseudocode? (/ is integer division.)
Integer c = 0
for (n = 1 to 50)
    Integer d = 0
    for (k = 1 to n)
        if (n mod k == 0)
            d = d + 1
        end if
    end for
    if (d == 3)
        c = c + 1
    end if
end for
Print c
  1. A. 7
  2. B. 15
  3. C. 4
  4. D. 3

Correct answer: C. 4

A number has exactly 3 divisors only if it is the square of a prime p (divisors 1, p, p²). Prime squares up to 50: 4, 9, 25, 49 → 4. '15' counts primes (2 divisors); '7' counts all perfect squares up to 49.

More Pseudocode and Code Tracing questions

Exams that ask this topic