r/javahelp • • Mar 27 '21

Solved 1%2=1?

i dontunderstand the results when the nr to the right is larger

ofc 4%3=1

but why 3%4=3

2 Upvotes

13 comments sorted by

View all comments

-5

u/FilsdeJESUS Mar 27 '21

3%4 = 3 how this is even possible ?

3

u/ATE47 Intermediate Brewer Mar 27 '21

3 = 4 * 0 + 3 (n = d * k + r), so we have 3 / 4 = 0 and 3 % 4 = 3

1

u/FilsdeJESUS Mar 27 '21

Ah yes it is a modulo operator 😂 the rest of the operation will be 3 that’s it

3

u/ATE47 Intermediate Brewer Mar 27 '21

It’s not really the modulus, it’s the remainder.

-2 mod 3 = 1

-2 % 3 = -2

(In Java, in other programming language it can be the modulus)