ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned char comp[51] = {0};
    int count = 0;
    for (int i = 2; i <= 50; i++) {
        if (comp[i]) continue;
        count++;
        for (int j = i * i; j <= 50; j += i)
            comp[j] = 1;
    }
    printf("%d", count);
}
  1. A. 15
  2. B. 14
  3. C. 16
  4. D. 25

Correct answer: A. 15

This sieve marks composites and counts the unmarked numbers from 2 to 50, which are the 15 primes up to 50. Counting 1 as prime gives 16.

More C Programming questions

Exams that ask this topic