Question #54907

#include<stdio.h>
int main()
{
int i=1,j=1;
for(;;)
{
if(i>5)
break;
else
j+=i;
printf("\n %d",j);
i+=j;
}
what is the output of this program sir? they have given the value of i as 5. please explain
1

Expert's answer

2015-09-26T00:00:46-0400

Answer on Question #54907 – Programming – C

Condition

What is the output of this program sir? They have given the value of i as 5. Please explain.


int main()
{
    int i = 1, j = 1;
    for (;;)
    {
        if (i > 5)
            break;
        else
            j += i;
        printf("\n %d", j);
        i += j;
    }
    return 0;
}

Solution

There was the error in the code, so I added the "}" before "return 0". You declare variable i with value 1 and variable j with value 1. for(;;) - it is infinite loop. In loop body you have condition – if variable i is more than 5, you will go out from loop body. If variable i is less than 5 or is equal 5, program will go to another way. j += i it is the same that j = j + i and i += j it is the same that i = i + j. In the first loop iteration after line j += i variable j will have value 2 and will print it. After line i += j variable i will have value 3. In the second loop iteration after j += i line variable j will have value 5 and will print it. After line i += j variable i will have value 8. In the third loop iteration i is more than 5, so loop will break (if condition) and program will finish.

Answer

Output:

2

5

http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS