-1

How java calculate remainder if left operand is smaller than left operand?

public class ModulusTest {

    public static void main(String[] args) {

        int a = 3 , b = 10;

        int modul1 = b%a;
        System.out.println("b modulus a = " + modul1);

        int modul2 = a%b;
        System.out.println("a modulus b = " + modul2);

    }
}

Output:

b modulus a = 1
a modulus b = 3

Looks like it returns only left operand?

Gerardas
  • 338
  • 5
  • 9

2 Answers2

0

What do you mean "left operand"?

The modulo of 3/10 is 3 that is

0*10 + 3

while the modulo of 10/3 is 1 that is

3*3 + 1

What's the problem?

Eypros
  • 5,370
  • 6
  • 42
  • 75
0

If left operator is lesser than the right operand in the mod(remainder) operation,Java language specifies that the result will always be the left-hand smaller operand.

Hence,

3%10=3 

in Java...

So,YOU CAN'T GO AGAINST LANGUAGE SPECIFICATIONS!!!

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73