+ 1

this question is from challenge

#include <iostream> using namespace std; int main() { int x=21,y=31 ,z; z=(x%5)*9/(y%6)*9; cout << z; return 0; } //according to me answer should be 1 but it was showing 81

19th Jul 2017, 3:23 AM
‎ ‏‏‎Anonymous Guy
10 Respuestas
+ 11
81 is the correct ans (21%5)*9/(31%6)*9 1*9/1*9=81 Because multiplication and division have same priority so it execute left to right
19th Jul 2017, 3:32 AM
Silambarasan T
Silambarasan T - avatar
+ 9
brackets is the high priority and then * and /. first calculate inside the bracket and goes from left to right
19th Jul 2017, 3:43 AM
Silambarasan T
Silambarasan T - avatar
+ 6
@Mayur 1*9/1*9 9/1*9. 9*9. //9/1=9 81
19th Jul 2017, 3:45 AM
Silambarasan T
Silambarasan T - avatar
+ 4
thank u @mayur @silambarasan for your help
19th Jul 2017, 3:44 AM
‎ ‏‏‎Anonymous Guy
+ 3
first division is done then multiplication is done z=(21%5)*9/(31%6)*9 z=1*9/1*9 z=1*9*9 z=9*9 z=81
19th Jul 2017, 3:33 AM
Mayur Chaudhari
Mayur Chaudhari - avatar
+ 3
I was confused by the brackets
19th Jul 2017, 3:39 AM
‎ ‏‏‎Anonymous Guy
+ 3
I got it
19th Jul 2017, 3:44 AM
‎ ‏‏‎Anonymous Guy
+ 3
Nope answer is 81. Look into order of operations and operator precedence. Parentheses come first: (x%5) = 1 (y%6) = 1 results to: 1 * 9 / 1 * 9 Multiplication and Division have the same precedence so they evaluate left to right: 1 * 9 = 9 results to: 9 / 1 * 9 9 / 1 = 9 results to: 9 * 9 = 81
19th Jul 2017, 3:47 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
ok lets see 1*9/1*9 // here 9/1 is done first then other operations not multiplication like 1*9 =1*9/9 =1*1 =1
19th Jul 2017, 3:39 AM
Mayur Chaudhari
Mayur Chaudhari - avatar
+ 1
in 1st step why 1*9 done first
19th Jul 2017, 3:39 AM
Mayur Chaudhari
Mayur Chaudhari - avatar