int p=15, q=12, s;
s=p/q*++p%(++p%--q);
System.out.print(s);
I am trying it according to the precedence of operators and associativity but I think it should give me the output 2
but it is giving me 4
as output.
int p=15, q=12, s;
s=p/q*++p%(++p%--q);
System.out.print(s);
I am trying it according to the precedence of operators and associativity but I think it should give me the output 2
but it is giving me 4
as output.
p/q*++p%(++p%--q)
is computed as
((p / q) * (++p)) % ((++p) % (--q))
using the operator precendece rules.
Note that ++x
/--x
increment/decrement the variable by one and then return the result after the operation. Since they change the variable itself, the effect obviously carries over to other usages of the variable.
It is then evaluated left to right, the math is
((15 / 12) * (16)) % ((17) % (11))
which results in 4
:
((15 / 12) * (16)) % ((17) % (11))
= (1 * 16) % 6
= 16 % 6
= 4