Bird
0
0
DSA Cprogramming~20 mins

Check if Number is Prime in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prime Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of prime check for 29
What is the output of this C code when checking if 29 is prime?
DSA C
#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int num = 29;
    if (isPrime(num))
        printf("%d is prime\n", num);
    else
        printf("%d is not prime\n", num);
    return 0;
}
ACompilation error
B29 is not prime
C29 is prime
DRuntime error
Attempts:
2 left
💡 Hint
Check divisors from 2 up to square root of the number.
Predict Output
intermediate
2:00remaining
Output of prime check for 1
What is the output of this C code when checking if 1 is prime?
DSA C
#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int num = 1;
    if (isPrime(num))
        printf("%d is prime\n", num);
    else
        printf("%d is not prime\n", num);
    return 0;
}
A1 is not prime
B1 is prime
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Remember prime numbers are greater than 1.
🔧 Debug
advanced
2:00remaining
Find the bug in prime check function
What is the output of this C code when checking if 4 is prime?
DSA C
#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i < n / 2; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int num = 4;
    if (isPrime(num))
        printf("%d is prime\n", num);
    else
        printf("%d is not prime\n", num);
    return 0;
}
A4 is prime
BCompilation error
CInfinite loop
D4 is not prime
Attempts:
2 left
💡 Hint
For n=4, n/2=2. Does the loop check i=2?
Predict Output
advanced
2:00remaining
Output for prime check of 49
What is the output of this C code when checking if 49 is prime?
DSA C
#include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int num = 49;
    if (isPrime(num))
        printf("%d is prime\n", num);
    else
        printf("%d is not prime\n", num);
    return 0;
}
ARuntime error
B49 is prime
CCompilation error
D49 is not prime
Attempts:
2 left
💡 Hint
49 is 7 times 7.
🧠 Conceptual
expert
2:00remaining
Why check divisors only up to square root?
Why does the prime check function only test divisors up to the square root of the number?
ABecause numbers larger than the square root are always prime
BBecause any factor larger than the square root would have a matching smaller factor already checked
CBecause checking beyond the square root causes runtime errors
DBecause the square root is the largest possible divisor
Attempts:
2 left
💡 Hint
Think about pairs of factors.