Question discussion

What will be the output of the following C code?

358862Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the following C code?
#include <stdio.h>
int main() {
    int i;
    for (i = 1; i <= 10; i++)
    {
        if (++i % 2 == 0)
            printf("%d ", i);
    }
    return 0;
}
  • A1 2 3 4 5 6 7 8 9 10
  • B2 4 6 8 10
  • C1 3 5 7 9
  • D2 4 6 8
Correct answer: B

Explanation

i = 1: ++i gives 2, which is even, so 2 is printed; the loop's i++ makes it 3. Then ++i = 4 is printed, and so on. The output is 2 4 6 8 10.

Discussion 0

Nobody has replied yet. Start the discussion!