+ 2
what is the output of this code? int b[]= {4,2,7,3}; int p=1; for (int i=0;i<4;i++){ if (i%2!=0) p*=b[i]; } cout<<p;
8 Réponses
+ 2
I believe what you are asking is what is the value of p*, and the answer is 3.
1. - The for loop executes for the values 0, 1, 2 and 3, but only in 2 of these values the condition i%2 != 0 is true, the values 1 and 3.
2. - Since the value 3 is executed after the value 1, the "final value" of i will be 3, so p* = b[3] = 3.
+ 1
the value of p is 6...first i = 0 and if i % 2 not equal to zero then it will multiply the p with value of 1 to to the array of b with position of the i so..
i = 0
0 % 2 = 0
i == 0 which is false..this will be ignored in the if statement
i = 1
1 % 2 = 1
i != 0 which is true..this will multiply p by the array with position i (which means 2) so now p = 2 cuz p is 1 so 1 * 2 = 2..
i = 2
2 % 2 = 0
i == 0 which is false..this will be ignored in the if statement
i = 3
3 % 2 = 1
i != 0 which is true..this will multiply p by the array with position i (which means 3) so now p = 6 cuz p is now 2 so 2 * 3 = 6..
i = 4
but i is not lower than 4 so it breaks the for loop..and then print the variable p..which is 6..
Hope you understand.. :)
0
I think answer is 1.
0
i believe it's 6
0
bt how?
0
yaah.. you write answer should be 6..thnks
0
answer is 6
0
This should print 6