mathinteger-divisionremainder

What is the remainder of integer division?


My task is to divide two variables as integer division with remainder. The problem is, that I have no clue what a “remainder” is, for now I did something like that, this is what I found by searching through the internet:

int a;
int b;
int remainder = Math.pow(a, 2) % b;

System.out.println("a^2 / b = " + Math.pow(a, 2) / b);
System.out.println("remainder = " + remainder);

If for example I set a = 43 and b = 67, I get this result:

a^2 / b = 27
remainder = 40

Now, since I have no idea what the remainder is (this is just a suggestion form the internet) I have no idea if this is the correct answer.

I was searching around about this topic but I still don't get it, if someone can elaborate I would be very thankful.


Solution

  • If you are looking for the mathematical modulo operation you could use

    int x = -22;
    int y = 24;
    System.out.println(Math.floorMod(x, y));
    

    If you are not interested in the mathematical modulo (just the remainder) then you could use

    int x = -22;
    int y = 24;
    System.out.println(x%y);