Reputation: 7
This is a question on a test I have, I'm having trouble understanding the solution which is:
01
12
23
(Which is the output of the program below)
int main()
{
int i, j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
printf("%d", i+j);
printf("\n");
}
return (0);
}
Can anyone explain why this happens? I'm completely stumped.
Upvotes: 0
Views: 71
Reputation: 13563
@Barmar is right but maybe you don't know how to substitute yourself as a computer.
Let me show you how I would do it myself.
I get to the inner loop and see that j successively is 0, 1. In the other hand, i is a constant for that loop.
Within the inner loop, we simply display the sum i+j as a digit, two times, that is i+0 followed by i+1, then print new line.
Now, the outer loop increments i from 0, 1, 2 successively.
As for the inner loop, I apply what I've found, and the program will end up, successively with:
01 12 23
Hope it helps.
Upvotes: 1