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);
}- A. 10
- B. 20
- C. 5
- 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
- What is printed? c #include <stdio.h> int main(void) { int m[2][2] = {{3, 1}, {4, 2}}; int s = 0; for (int i = 0; i < 2; i++) { int lo =…
- What is printed? c #include <stdio.h> int main(void) { int r0[2] = {1, 2}, r1[2] = {3, 4}; int rows[2] = {r0, r1}; int t = rows[0]…
- What is printed? The reverse of the binary digits of 13 (1101) is taken as an integer. c #include <stdio.h> int main(void) { unsigned x =…
- How many occurrences of the two-letter string "ab" are in "abcabab"? Count them with this program. c #include <stdio.h> int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; switch ('b') { case 'a': c++; case 'b': c += 10; case 'c': c += 100…
- How many nodes are at depth 2 in a perfect binary tree of depth 3 (the root is depth 0)? The program counts them recursively. c #include…