What will be the output of the following C code? int main() {
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;
}
Show answerHide answer
Correct answer: (B) 2 4 6 8 10
Explanation
++i inside the if makes i even each time it is tested: i goes 1 to 2 (printed), then the loop's i++ makes 3, ++i makes 4 (printed), and so on up to 10. Then i becomes 11 and the loop stops, so 2 4 6 8 10 is printed.