+ 2
Why is the output 6 of this code?
#include <iostream> using namespace std; int main() { int i,x; i = 0; x = 1; for(int x=1; x<4; x++) i++; x++; i*=x; cout << i; return 0; }
1 Answer
+ 12
In the for loop, i gets incremented three times, and i stores the value of 3. Variable x gets incremented once, to become 2. We then multiply i with x and store the result in i. We print the value of i, which is 2*3, hence getting 6.