0
Class, please explain to me:
int i; if ((i%2==0) && (i%4==0)) will give the same output with the following if statement: if (i%8==0); Thank you.
6 Respostas
+ 3
Oliver Pasaribu
As there is && so only number divided by (2 * 4 = 8) will be print in 1st case which is similar to 2nd case.
If there is or (||) then outputs will be different in both cases.
or (||) means number divided by either 2 or 4
+ 3
Oliver Pasaribu only the lowest common multiple (LCM) of 2 and 4 will give the same result. So in fact, (i%8==0) is not the same.
The LCM of 2 and 4 is 4, so only (i%4==0) is the same. Proof: try i=12. It passes for (i%2==0 && i%4==0), and (i%4==0), but fails for (i%8==0).
It works because the value of i must be a multiple of both factors (2 and 4) in order for the remainder to be zero for both. Therefore, by using the modulo of the lowest common multiple, you are assured that it is also a multiple of both.
+ 1
Does (i%4)&&(i%2) equivalen with (i%8);
i%4 means each int value of i that is divisible by 4 (result integer part only, no remainder, i%4=0)
divisible by 4: 0,4,8,12,16,20,24, ...
divisible by 2:
0,2,4,6,8,10,12,14,16,18,20,..
divisible by 8:
0,8,16,24,32,40,48,56,64,72,80,...
So, .divisible by 2 AND 4 must be:
4,8,12,16,20,24,28,e2,36,40,...
divisible be 8:
0,8,16,24,32,40,48,56,64,72,80
Oh I see, this two statemen is not biimplication A<->B.
1. If (i%8==0) then : ((i%4==0)&&(i%2==0))
statement 1 is true.
But
2. if ((i%4==0)&&(i%2==0)) then (i%8==0).
Statement 2 is incorrect.
+ 1
Yes, thank you AJ and Brian. I think it is clear now that is two different problem. The sequence is also affrcts it.
Yes.
(i%8==0) ->
((i%2==0)&&(i%4==0)
This is true.
But:
((i%2==0)&&(i%4==0) ->
(i%8==0).
This is incorrect.
+ 1
Ok.
0
Of course no. Try with i = 12; the first test will be True whereas the second will be False.