+ 4

Assignment operators

modules is divided remainder,but i cant understand why the output is 4?, modules 4 %= 2 is 0, modules 4 %= 4 also 0,so 0+0=0, why is 4? public class Program { public static void main(String[] args) { int num1 = 2; int num2 = 4; num2 %= num1 + num2; System.out.println(num2); } }

19th Jan 2019, 2:54 PM
Tzion
Tzion - avatar
6 Answers
+ 4
Because num2 %= num1 + num2 is num2 = num2 % (num1 + num2) num1 + num2 = 2 + 4 is 6 4 div 6 is 0 with remainder 4 So 4 is stored in num2
19th Jan 2019, 3:30 PM
Gordon
Gordon - avatar
+ 4
Demo: https://code.sololearn.com/cFOrbbXjg2Hw/?ref=app If 9 div 2 remainder is 1 because the largest multiple of 2 smaller than 9 is 8 ( 2 times 4 ) If a smaller number is dividing a larger number, like as you calculated, resulting a decimal smaller than 1, means no multiple of the larger number is smaller than the smaller number. 6 x 1 is already larger than 4 So the largest multiple becomes 6 x 0 which is 0, and the whole 4 becomes the remainder. This is the same for any situation when a smaller number is dividing a larger number. Such as 2%5 8%300 100%10000, the true div result 0 and the remainder will be 2 8 100 respectively.
20th Jan 2019, 3:16 AM
Gordon
Gordon - avatar
+ 3
(A % B = r) because (A = B x q + r) note that (r > 0) r should be always positive!! exemples: (4 % 2 = 0) because (4 = 2 x 2 + 0) (4 % 3 = 1) because (4 = 3 x 1 + 1) (4 % 4 = 0) because (4 = 4 x 1 + 0) (4 % 5 = 4) because (4 = 5 x 0 + 4) we cant use 5 x 1 or higher because 5 x 1 + (- 1) and here we have r = - 1 and we say r positive thats way 5 x 0 is correct one. sorry for bad english 😔
19th Jan 2019, 3:45 PM
Azeddine Hamdaoui
Azeddine Hamdaoui - avatar
+ 2
I think he gets this quiz from 5-question challenge
19th Jan 2019, 3:32 PM
Gordon
Gordon - avatar
+ 1
you should write like this num2 %= num1 + num2%num2 or num2 = num2%num1 + num2%num2
19th Jan 2019, 3:30 PM
Ahmet
0
Gordon i calculate using calculater 4divide 6 is 0.6666666666666 , i dont understand why is remainder 4?
19th Jan 2019, 11:44 PM
Tzion
Tzion - avatar