Java Remainder of Integer Divison?

DisasterCoder picture DisasterCoder · Sep 25, 2015 · Viewed 56.1k times · Source

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

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 I for example set (a = 43) and (b = 67)

Then I will get this reslut:

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..?

Thanks for any help,

Kind Regards

Answer

SomeJavaGuy picture SomeJavaGuy · Sep 25, 2015

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);