floorDiv() int in Java

floorDiv(): This method is available in the Math class of Java.

Syntax:

int java.lang.Math.floorDiv(int x, int y)

This method takes two parameters of type int. This method returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient. There is one special case if the dividend is the Integer.MIN_VALUE and the divisor are -1, then integer overflow occurs and the result is equal to Integer.MIN_VALUE.

1. If the signs of the arguments are the same, the results of floorDiv and the / operator are the same. For example, floorDiv(4, 3) == 1 and (4 / 3) == 1.

2. If the signs of the arguments are different, the quotient is negative and floorDiv returns the integer less than or equal to the quotient and the / operator returns the integer closest to zero. For example, floorDiv(-4, 3) == -2,whereas (-4 / 3) == -1. 

Parameters: Two parameters are required for this method.

x: the dividend.

y: the divisor.

Returns: the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

Note:  It Throws: ArithmeticException - if the divisor y is zero.

For Example:

Math.floorDiv(13,3) = > It returns 4.

Approach

Java

public class FloorDivInt {
    public static void main(String[] args) {

        int x = 13, y = 3;
        
        System.out.println(Math.floorDiv(x, y));
    }
}

Output:

4

No comments:

Post a Comment