+ 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); } }

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

+ 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.

+ 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 😔

+ 2

I think he gets this quiz from 5-question challenge

+ 1

you should write like this
num2 %= num1 + num2%num2 or
num2 = num2%num1 + num2%num2

0

Gordon i calculate using calculater 4divide 6 is 0.6666666666666 , i dont understand why is remainder 4?

Hot today

I have made a calculator in which my % (Percentage) not work correctly for 100%50 or 100%20.

2 Votes

Python palindrome challenge.

1 Votes

Java

0 Votes

Number of Ones ( C++ ) question!

1 Votes