Write the value of sum1 after execution of the
following WHILE
loop :
int i = 1,
sum1 = 0;while (i<10){
sum1 =sum1+ i;i =i+2;}
Working of the code is given below:
Given int i=1 ; and sum1=0 | ||||
From code | sum1= sum1+i | Value of i-at this point | ||
= 0 +1 = 1 | Due to i=i+2 | 1+2=3 | ||
Now i=3 | which is < 10 | = 1 +3 = 4 | Due to i=i+2 | 3+2=5 |
Now i=5 | which is < 10 | = 4+5 = 9 | Due to i=i+2 | 5+2=7 |
Now i=7 | which is < 10 | = 9+7 = 16 | Due to i=i+2 | 7+2=9 |
Now i=9 | which is < 10 | = 16+9 = 25 | Due to i=i+2 | 9+2=11 |
Now i=11 | which is not < 10 so the code ends. | Output =25 |
Welcome to our educational Website!
EmoticonEmoticon