Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Remainder of Integer Divison?

Tags:

java

math

divide

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

like image 571
DisasterCoder Avatar asked Sep 25 '15 09:09

DisasterCoder


People also ask

How do you divide a remainder in Java?

Get the remainder using % operator. Expressions used in program to calculate quotient and remainder: quotient = dividend / divisor; remainder = dividend % divisor; Note: The program will throw an ArithmeticException: / by zero when divided by 0.

What find the remainder of an integer division?

In integer division and modulus, the dividend is divided by the divisor into an integer quotient and a remainder. The integer quotient operation is referred to as integer division, and the integer remainder operation is the modulus.

How do you return a remainder in Java?

int remainder = a % b; will sort you. The remainder operator returns the remainder of the division. Note that the remainder operator is also called the modulo operator.


1 Answers

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);
like image 162
SomeJavaGuy Avatar answered Sep 19 '22 02:09

SomeJavaGuy