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);
}- A. 6
- B. 7
- C. 48
- 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
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 1u << 31; x <<= 1; printf("%u", x); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'n' == 10 && '0' == 0); }
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 0x1F; uint8t r =…
- What is printed? c #include <stdio.h> #include <stdlib.h> int ds(int n) { int s = 0; while (n) { s += n % 10; n /= 10; } return s; } int…
- How many ordered ways are there to write 6 as a sum of parts 1, 2 and 3? Compute with this program. c #include <stdio.h> int f(int n) { if…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 7; int i = 0; while (x & 1u) { x >>= 1; i++; } printf("%d", i); }