Why does this problem print 4? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

Why does this problem print 4?

int a = 9%5%5;

29th Aug 2019, 3:20 PM
Juan Debenedetti
Juan Debenedetti - avatar
9 Answers
+ 13
The answer is here with explaination To find 4 mod 5 using the Modulus Method, we first find the highest multiple of the Divisor (5) that is equal to or less than the Dividend (4). Then, we subtract the highest Divisor multiple from the Dividend to get the answer to 4 modulus 5 (4 mod 5): Multiples of 5 are 0, 5, 10, 15, etc. and the highest multiple of 5 equal to or less than 4 is 0. Therefore, to get the answer: 4 - 0 = 4
30th Aug 2019, 3:54 PM
Rohitman
Rohitman - avatar
+ 9
Sorry for giving you wrong answer.
30th Aug 2019, 3:53 PM
Rohitman
Rohitman - avatar
+ 9
9%5=4 4%5=4 So the output is 4
30th Oct 2019, 5:46 AM
Lavanya Yadav
Lavanya Yadav - avatar
+ 5
The roll of % is remainder: It divides the number until it can't divide anymore so if you try to divide 4 by 5 you can't so it I'll return 4
29th Aug 2019, 3:31 PM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
+ 3
(Natural number is any non-negative whole number: 0, 1, 2,...) 9%5: Unlimitely subtracts/adds 5 from 9 until 9 is the smallest possible natural number. 9-5 = 4 4 is the smalles possible natural number, because if you subtracted 5 again: 4-5 = -1 -1 is not a natural number. When 9%5 = 4: 9%5%5 = 4%5: 4 is already the smallest possible natural number, when 4 got unlimitely added/subtracted 5, then the result is 4. x%y%y = x%y X%Y: Unlimitely subtracts/adds Y from X until X is the smallest possible natural number.
29th Aug 2019, 3:52 PM
Seb TheS
Seb TheS - avatar
+ 2
9%5=4%5=4
29th Aug 2019, 3:22 PM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
+ 2
Eliya Ben Baruch 4%5=4 How? Rest -> 0
29th Aug 2019, 3:29 PM
Juan Debenedetti
Juan Debenedetti - avatar
+ 1
Rohit Gupta are you sure that 4%5 = 0?
30th Aug 2019, 3:44 PM
Seb TheS
Seb TheS - avatar
+ 1
'%' is modulus. These operators gives the remainder of any division. In above example, 9%5%5, the first part, i.e. 9%5 gives remainder 4. In the next part, 4%5 will give you answer 4 once more. Thus, the answer is 4. Hope it helps 😊
31st Aug 2019, 3:53 PM
n_y_1411
n_y_1411 - avatar