+ 1

Just ran into this C++ question and needed help understanding it. Please help

The code is written down below. I know the answer is 10 but I couldn't find out why it is so. Int a= 10, b; B=a%(a+(a+1)/a); Cout<<b;

19th Aug 2019, 3:54 AM
Nahom
Nahom - avatar
4 ответов
+ 5
Its the operator precedence in the expression. In the 2nd line,the brackets are evaluated first,which is followed by division operation and then addition. So,the evaluation is done as follows: 10%(10+(10+1)/10) =10%(10+(11)/10) =10%(10+1) =10%11 =10 Hence, b=10;
19th Aug 2019, 4:02 AM
Arnold D'Souza
Arnold D'Souza - avatar
+ 4
Just pedantically saying that you should have used a lower case b and cout rather than Cout.
19th Aug 2019, 4:45 AM
Sonic
Sonic - avatar
+ 3
1. (a + 1) / a = 11 / 10 = 1 2. a + (a + 1) / a = a + 1 = 11 3. a % (a + (a + 1) / a) = 10 % 11 = 10
19th Aug 2019, 4:02 AM
unChabon
unChabon - avatar
+ 2
Thanks guys!
19th Aug 2019, 4:03 AM
Nahom
Nahom - avatar