Does someone help please with the modulo operator in Java why 15 % 6 is equal 3?


#1

Why 15 % 6 is equal 3 ?


#2

because the modulo operator gives you the remainder, the number left and after the biggest possible division.

12 is divisible by 6 and smaller then 15
18 is divisible by 6 but not smaller then 15

so 12 is the biggest number divisible by 6, so we have 3 remaining (15 - 12= 3)


#3

To put it another way, the modulo operator shows only the remainder and ignores the quotient.
6 goes into 15 twice, up to 12, yes? and from 12 to 15, the remainder is 3.


#5

When I used BASIC on the ZX Spectrum years ago there was no modulo function.
There was as INT function which just returned the integer part of a decimal number
e.g INT 3.123 = 3.

So I did modulo like this , X mod Y = ( X/Y - INT (X/Y)) * X

15 % 6 = (15/6 - INT (15/6)) * 6 =( 3.5 - 3) * 6 = 3

It’s basic numeracy and a bit of computer history!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.