Question discussion

What is the output of the following C program?#include int main() { int i = 5; while (i > 0) { i -= 2; } printf("%d", i); return 0; }

1965Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
int main() {
    int i = 5;
    while (i > 0) {
        i -= 2;
    }
    printf("%d", i);
    return 0;
}
  • A0
  • B-1
  • C1
  • D5
Correct answer: B

Explanation

The value of i goes 5, 3, 1 and then −1, at which point the condition i > 0 fails, so −1 is printed.

Discussion 0

Nobody has replied yet. Start the discussion!