ProgrammingHardTCS NQT · Infosys

How many odd multiples of 3 are there in 1 to 30? Count them with this program.

How many odd multiples of 3 are there in 1 to 30? Count them with this program.
#include <stdio.h>
int main(void) {
    int c = 0;
    for (unsigned n = 1; n <= 30; n++)
        if (n % 3 == 0 && n % 2) c++;
    printf("%d", c);
}
  1. A. 10
  2. B. 20
  3. C. 5
  4. D. 15

Correct answer: C. 5

n % 3 == 0 && n % 2 keeps the odd multiples of 3: 3, 9, 15, 21, 27, so c = 5. 10 ignores the oddness test, 15 counts all odd numbers, and 20 reads && as || (10 + 15 - 5).

More C Programming questions

Exams that ask this topic