Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the array that marks prime numbers.
DSA C
int prime[[1]]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using size n instead of n+1 causes out-of-bound errors.
Using a fixed size like 100 limits the range.
✗ Incorrect
We need an array of size n+1 to mark numbers from 0 to n.
2fill in blank
mediumComplete the code to initialize all entries as true (1) in the prime array.
DSA C
for (int i = 0; i <= n; i++) { prime[i] = [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting to 0 marks all as not prime initially.
Using negative values is incorrect for boolean flags.
✗ Incorrect
We mark all numbers as prime initially by setting to 1 (true).
3fill in blank
hardFix the error in the loop condition to run only up to the square root of n.
DSA C
for (int p = 2; p [1]= sqrt(n); p++) { if (prime[p] == 1) { for (int i = p * p; i <= n; i += p) { prime[i] = 0; } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' misses markings when sqrt(n) is integer (perfect square n).
Using '>=' or '>' reverses the loop and causes no iterations.
✗ Incorrect
We loop while p is less than or equal to the square root of n.
4fill in blank
hardFill both blanks to print all prime numbers after sieving.
DSA C
for (int i = [1]; i <= n; i++) { if (prime[i] == [2]) { printf("%d ", i); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 or 1 prints non-prime numbers.
Checking for prime[i] == 0 prints non-primes.
✗ Incorrect
We start from 2 because 0 and 1 are not prime, and print numbers marked as 1 (prime).
5fill in blank
hardFill all three blanks to complete the sieve function header and variable declaration.
DSA C
[1] sieve(int [2]) { int [3][n+1]; // rest of the code }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using int as return type without return statement.
Wrong parameter name or type.
Incorrect array name.
✗ Incorrect
The function returns nothing (void), takes int n as input, and declares int prime array.
