In the following C program fragment, j, k n and TwoLog_n are interger variables, and A is an array of integers. The variable n is initialized to an integer ≥ 3, and TwoLog_n is initialized to the value of 2*⌈log2(n)⌉
for (k = 3; k < = n; k++) A[k] = 0; for (k = 2; k < = TwoLog_n; k++) for (j = k + 1; j < = n; j++) A[j] = A[j] || (j % k); for (j = 3; j < = n; j++) if (!A[j]) printf ( "%d" , j); |
The set of numbers printed by this program fragment is
(A) {m | m ≤ n, (∃ i) [m = i!]} Here i! mean factorial of i
(B) {m | m ≤ n, (∃ i) [m = i2]}
(C) {m | m ≤ n, m is prime}
(D) {}
Answer: (D)
Explanation: Option (D) is correct because if take n=4 then TwoLog_n value 4.
first loop initially A[3]=A[4]=0;
then two loop k = 2 to 4 and j = 3 to 4
if k=2 j=3 A[3] = A[3] || (3%2) = 0 || 1 A[3] = 1 j=4 A[4] = A[4] || (4%2) = 0 || 0 A[4] = 0 K=3 j=4 A[4] = A[4] || (4%3) = 0 || 1 A[4] = 1 K=4 J=5 condition false means terminate A[3] = A[4] = 1
Means last print loop never execute.
Quiz of this Question