ProgrammingHardProduct company OA
What is printed?
What is printed?
char comp[31] = {0};
int cnt = 0;
for (int i = 2; i <= 30; i++) {
if (comp[i]) continue;
cnt++;
for (int j = i * i; j <= 30; j += i) comp[j] = 1;
}
printf("%d", cnt);- A. 11
- B. 10
- C. 9
- D. 15
Correct answer: B. 10
This is the sieve of Eratosthenes: each prime marks its multiples from i·i on (smaller multiples were already marked by smaller primes). The primes up to 30 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29: 10 of them.
More C Programming questions
- What is printed? c int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main(void) { int l = 1; for (int i = 1; i <= 10; i++) l = l…
- What is printed? c int isqrt(int n) { int lo = 0, hi = n, r = 0; while (lo <= hi) { int m = lo + (hi - lo) / 2; if ((long long)m m <= n) {…
- What is printed? c struct N { struct N nx; } n[5]; for (int i = 0; i < 4; i++) n[i].nx = &n[i + 1]; n[4].nx = &n[2]; struct N s = n, f =…
- On a two's-complement machine with C99 division, what is printed? c printf("%d %d", -13 % 8, -13 & 7);
- Assume IEEE-754 single-precision float and 32-bit unsigned int. What is printed? c union { float f; unsigned u; } v = {1.0f}…
- What is printed? c #line 100 int a = LINE; int b = LINE; printf("%d %d", a, b);