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; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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;

20th Dec 2016, 4:31 PM
kirti
kirti - avatar
8 Answers
+ 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.
20th Dec 2016, 4:53 PM
Syvered
Syvered - avatar
+ 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.. :)
20th Dec 2016, 6:15 PM
Fluffy Rabbit
Fluffy Rabbit - avatar
0
I think answer is 1.
20th Dec 2016, 4:44 PM
kirti
kirti - avatar
0
i believe it's 6
20th Dec 2016, 4:47 PM
Nikunj Arora
Nikunj Arora - avatar
0
bt how?
20th Dec 2016, 4:49 PM
kirti
kirti - avatar
0
yaah.. you write answer should be 6..thnks
21st Dec 2016, 9:33 AM
kirti
kirti - avatar
0
answer is 6
6th Jan 2017, 7:53 AM
Karishma Kumari
Karishma Kumari - avatar
0
This should print 6
11th Jan 2017, 11:25 AM
Pratik Nakave
Pratik Nakave - avatar