-3

Use a modulus operator is something which all programmers must to know. I know it =).

In java we have :

int a = 100 , b = 50, c;

If we do :

c = a % b; // c = 0 because : 100 = 50*2 + 0 | D = d*q + r using simple maths

However I felt a little frustrated for not finding the Why of this operation :

c = b % a; // c = 50 ???? It seems not to have logic when a use D = d*q + r

Can someone could explain me why 50 % 100 is 50 ??? I can't understand very well.

Thanks

afym
  • 532
  • 2
  • 6
  • 22

2 Answers2

8

Becuase you can multiply by 0:

c = 100*0 + 50;

It's the + 50 that is returned as modulo.

AntonH
  • 6,359
  • 2
  • 30
  • 40
3

Think of it this way:

100 goes into 50 how many times?

Zero times. So there must be 50 left over. Therefore the answer is 50.

ctzdev
  • 646
  • 2
  • 9
  • 24