ProgrammingHardTCS NQT · Wipro

How many numbers from 1 to 100 are multiples of 16? Compute with this program.

How many numbers from 1 to 100 are multiples of 16? Compute with this program.
#include <stdio.h>
int main(void) {
    int c = 0;
    for (unsigned n = 1; n <= 100; n++)
        if ((n & 15u) == 0) c++;
    printf("%d", c);
}
  1. A. 6
  2. B. 7
  3. C. 48
  4. D. 12

Correct answer: A. 6

n & 15 keeps the low four bits, which are all zero exactly for multiples of 16: 16, 32, 48, 64, 80, 96, i.e. 6. 48 tests n & 16 (bit 4 set), 12 counts multiples of 8, and 7 wrongly includes 0, which the loop skips.

More C Programming questions

Exams that ask this topic