+ 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
10 odpowiedzi
+ 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
+ 9
brackets is the high priority and then * and /. first calculate inside the bracket and goes from left to right
+ 6
@Mayur
1*9/1*9
9/1*9.
9*9. //9/1=9
81
+ 4
thank u @mayur @silambarasan for your help
+ 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
+ 3
I was confused by the brackets
+ 3
I got it
+ 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
+ 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
+ 1
in 1st step why 1*9 done first