Bird
0
0
DSA Cprogramming~10 mins

Check if Number is Prime 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 check if a number is less than 2, which is not prime.

DSA C
if (num [1] 2) {
    printf("Not prime\n");
    return 0;
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '<' to check for non-prime numbers.
Checking if num is greater than 2 here.
2fill in blank
medium

Complete the for loop to check divisors from 2 up to the square root of num.

DSA C
for (int i = 2; i [1] num / i; i++) {
    if (num % i == 0) {
        printf("Not prime\n");
        return 0;
    }
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '>=' which causes the loop to not run correctly.
Using '<' which can cause off-by-one errors in some cases.
3fill in blank
hard

Fix the error in the if condition to correctly check if num is divisible by i.

DSA C
if (num [1] i == 0) {
    printf("Not prime\n");
    return 0;
}
Drag options to blanks, or click blank then click option'
A%
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '/' instead of '%' which performs division, not remainder check.
Using '*' or '-' which are incorrect operators here.
4fill in blank
hard

Fill both blanks to complete the function header and return type for checking prime.

DSA C
[1] is_prime(int num) {
    if (num < 2) {
        return [2];
    }
    // rest of code
}
Drag options to blanks, or click blank then click option'
Aint
B0
Cvoid
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'void' as return type which cannot return a value.
Returning 1 for numbers less than 2 which is incorrect.
5fill in blank
hard

Fill all three blanks to complete the return statement that confirms the number is prime.

DSA C
return [1];

// Usage example:
// if (is_prime([2])) {
//     printf("%d is prime\n", [3]);
// }
Drag options to blanks, or click blank then click option'
A1
Bnum
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 which means not prime.
Using wrong variable names in usage example.