I know that in Python you can do floor division like this:
5 // 2 #2
The //
is used for something totally different in Java. Is there any way to do floor division in Java?
I know that in Python you can do floor division like this:
5 // 2 #2
The //
is used for something totally different in Java. Is there any way to do floor division in Java?
If you're using integers in the division and you cast the solution to another integer (by storing the result in another integer variable), the division is already a floor division:
int a = 5;
int b = 2;
int c = 5/2;
System.out.println(c);
>> 2
As khelwood stated, it also works with negative values but the rounding is done towards 0. For example, -1.7 would be rounded to -1.
You can do
OR
If you were to call
System.out.println(answer);
the output would be