×

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and then returns an integer that is less or equal to the quotient.

Syntax:

  1. public static int floorDiv(int x, int y)
  2. public static long floorDiv(long x, int y)
  3. public static long floorDiv(long x, long y)

Parameters:

The parameter ‘x’ and ‘y’ represents the dividend and the divisor.

Return Value:

The floorDiv () method returns the largest integer value that is less than or equal to the algebraic quotient.

  • It returns the result same as the first argument, if the first argument passed is equal to an integer or long value and second argument is -1.
  • It throws an Arithmetic Exception, if the divisor is zero.

Example 1:

public class JavaMathFloorDivExample1 {
    public static void main(String[] args) {
        int x = 2125;
        int y= 10;
        // divides x and y(x/y) and returns the integer less than or equal to
quotient.
        System.out.println(Math.floorDiv(x, y));
    }
}

Output:

212

Example 2:

public class JavaMathFloorDivExample2 {
    public static void main(String[] args) {
        int x = 2125;
        long y= 100;
        // divides x and y(x/y) and returns the integer less than or equal to
quotient.
        System.out.println(Math.floorDiv(x, y));
    }
}

Output:

21

Example 3:

public class JavaMathFloorDivExample3 {
    public static void main(String[] args) {
        long x = -2125;
        long y= 1;
        //it returns an Airthmetic Exception, if the Second argument is zero
        System.out.println(Math.floorDiv(x, y));
    }
}

Output:

-2125

Example 4:

public class JavaMathFloorDivExample4 {
    public static void main(String[] args) {
        long x = -2125;
        long y= -1;
        //it y is -1 then it will alter the sign of x
        System.out.println(Math.floorDiv(x, y));
    }
}

Output:

2125

Example 5:

public class JavaMathFloorDivExample5 {
    public static void main(String[] args) {
        int x = Integer.MIN_VALUE;
        int y= -1;
        /*if the dividend is the Integer.MIN_VALUE and the divisor is -1,
        then integer overflow occurs and the result is equal to Integer.
MIN_VALUE */
        System.out.println(Math.floorDiv(x, y));
    }
}

Output:

-2147483648

Related Topics

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Collection Programs in Java

Collection Programs in Java Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly...

4 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Java StringReader Class

StreamReader Class technique enables character reading from a string. The java.io package contains this class. A string serves as a source in the character stream. While the Stream class is...

4 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Java Viva Questions and Answers

Question: Explain Java programming language. Answer: It is a high-level programming language. This programming language is based on the principles of object-oriented programming. Large-scale applications can be developed by using this...

16 minutes read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Interchange Diagonal elements in Java

In this article, you will be very well acknowledged about what is a matrix with an example. You will also be acknowledged about how to interchange the diagonal elements in...

4 minutes read.

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int. Syntax: public static int toIntExact (long value) Parameters: The...

2 minutes read.