The Output is 19 of the below code, The question is "How" ?, I really don't get it ! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

The Output is 19 of the below code, The question is "How" ?, I really don't get it !

int a,b,c; a = 12; b = 7; c = 24; cout << c-a % b;

5th Apr 2023, 5:03 PM
Muhammad Bilal
Muhammad Bilal - avatar
7 Answers
+ 4
The output is 19 because of the operator precedence and associativity rules of C++. In the given code, the expression a % b is evaluated first because the modulus operator % has higher precedence than the subtraction operator -. So, a % b evaluates to 5. Then, the expression c - 5 is evaluated, which gives the result 19. This is because subtraction has left-to-right associativity, which means that the expression will be evaluated from left to right. Therefore, the final output is 19.
5th Apr 2023, 5:26 PM
Z3ro
+ 3
Okay okay got it, its like DMAS system 👍🏻. Thanks for the help :)
5th Apr 2023, 5:17 PM
Muhammad Bilal
Muhammad Bilal - avatar
+ 2
12 % 7 = 5 24 - 5 = 19
5th Apr 2023, 5:07 PM
A͢J
A͢J - avatar
+ 2
You have to study operator precedence to figure out what is going on 👍
5th Apr 2023, 5:24 PM
Z3ro
+ 1
Oky got it thanks :)
5th Apr 2023, 5:25 PM
Muhammad Bilal
Muhammad Bilal - avatar
+ 1
Yeah i already got it, it is similar to the DMAS (Divide/Multiply/Addition/Subtract) of what we studied in our Mathematics 😃.
5th Apr 2023, 5:29 PM
Muhammad Bilal
Muhammad Bilal - avatar
0
But it says that firstly subtract c & a and then 'remainders' their answers to the b. Am i saying right ?
5th Apr 2023, 5:15 PM
Muhammad Bilal
Muhammad Bilal - avatar