+ 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; }

16th Mar 2017, 1:07 AM
nabeer zahed chowdhury
nabeer zahed chowdhury - avatar
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.
16th Mar 2017, 1:19 AM
Hatsy Rei
Hatsy Rei - avatar