ProgrammingVery hardTCS Digital · Infosys InfyTQ

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function gcd(Integer a, Integer b)
    while (b != 0)
        t = a mod b
        a = b
        b = t
    end while
    return a
end function
Integer i, j, c = 0
for (i = 1; i <= 30; i = i + 1)
    for (j = i + 1; j <= 30; j = j + 1)
        if (gcd(i, j) == 1)
            c = c + 1
        end if
    end for
end for
Print c
  1. A. 278
  2. B. 300
  3. C. 277
  4. D. 271

Correct answer: C. 277

For each j from 2 to 30 the number of i < j coprime to j is Euler's phi(j); the sum of phi(2..30) is 277.

More Pseudocode and Code Tracing questions

Exams that ask this topic