0

Order of precedence

int main() int x = 5; int y = 2; int z = x - y * 2; Cout << z; Now it should be 1 but while I was solving an MCQ it gave me answer of -1. Is my mathematics that much weak?

12th Sep 2023, 8:16 AM
RAJIV KUMAR KULIYA
2 RĂ©ponses
+ 4
RAJIV KUMAR KULIYA , let's check:- the expression `x - y * 2` is evaluated according to the operator precedence rules. The multiplication operator `*` has higher precedence than the subtraction operator `-`. So, the expression is `x - (y * 2)` `5-(2*2)` First: `y * 2`= 2*2which gives 4. Second: `x - (y * 2)` =5 - 4 which gives 1. Therefore, the answer should be 1, not -1. Conclusion:- Your mathematics skills are not weak! It seems like there might be an error in the MCQ or in the way it was solved.
12th Sep 2023, 8:34 AM
Darpan kesharwani🇼🇳[Inactive📚]
Darpan kesharwani🇼🇳[Inactive📚] - avatar
+ 2
No, you're mathematics aren't that weak. The MCQ is wrong. We're supposed to do the multiplication one first, then subtraction in the following code.
12th Sep 2023, 8:32 AM
Dragon RB
Dragon RB - avatar