ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer n = 30, c = 0, j
for (each i from 1 to n)
j = i
while (j <= n)
c = c + 1
j = j + i
end while
end for
Print c- A. 120
- B. 111
- C. 465
- D. 96
Correct answer: B. 111
For each i the inner loop visits i, 2i, 3i, … ≤ 30, i.e. ⌊30/i⌋ times. So c = Σ ⌊30/i⌋, which also equals the total number of divisors of the numbers 1…30. Adding: 30 + 15 + 10 + 7 + 6 + 5 + 4 + 3 + 3 + 3 + 2×5 (i = 11…15) + 1×15 (i = 16…30) = 111. 465 = 1 + … + 30 treats the inner loop as running 31 − i times.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1; i <= 100; i = i 2) for (j = i; j <= 100; j = j + 1) c = c +…
- What will be the output of the following pseudocode? // 32-bit signed integers; overflow wraps around (two's complement); / truncates…
- What is the time complexity of the following pseudocode, as a function of n? for (i = n; i > 0; i = i / 2) for (j = 0; j < i; j = j + 1)…
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i <= n; i = i + 1) j = 1 while (j < i) j = j 2…
- What is the time complexity of the following pseudocode, as a function of n? i = 2 while (i <= n) i = i i end while
- What is the time complexity of the following pseudocode, as a function of n? for (i = 1; i i <= n; i = i + 1) for (j = 1; j <= n; j = j 2)…