0
please explain and obtain output of this array.
int b[]={1,4,9,5,7}; int sum=0; for(int i=0;i<5;i++){ if(i%2==0){ sum+=b[i]; } cout<< sum;
5 Réponses
+ 1
when i =0 sum=0+1
when i =1 but 1%2 is not equal to zero
when i= 2 sum = 1+9
when i = 3 but 3%2 is 1 which is not zero
when i = 4 sum = 7+10
then It comes out of loop and sum= 17 is printed
0
write the size in the array b as 5
when the value of i is even then at each even value of array is added in sum
when i=0
sum = 0+1;
when i=2
sum =1+9;
when i=4
sum =10+7;
the output is 17
and add one more curly Bracket before cout
0
there is an open bracket that has not been closed. the "for" one. to be able to compile it remove the opening parenthesis of "if" or put a closed one before the cout.
0
You have to structurate your code to be more ease comprensible.
0
4%2 is also 0.so why not it get included?