Bird
0
0
DSA Cprogramming~10 mins

Sieve of Eratosthenes Find All Primes in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
An+1
B100
Cn-1
Dn*2
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.
2fill in blank
medium

Complete 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'
A-1
Bn
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting to 0 marks all as not prime initially.
Using negative values is incorrect for boolean flags.
3fill in blank
hard

Fix 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'
A<=
B<
C>
D>=
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.
4fill in blank
hard

Fill 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'
A2
B1
C0
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 or 1 prints non-prime numbers.
Checking for prime[i] == 0 prints non-primes.
5fill in blank
hard

Fill 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'
Avoid
Bn
Cprime
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using int as return type without return statement.
Wrong parameter name or type.
Incorrect array name.