w3resource

Java: Compute the floor division and the floor modulus of the given dividend and divisor


Compute Floor Division and Modulus

Write a Java program to compute the floor division and floor modulus of the given dividend and divisor.

Sample Solution:

Java Code:

public class Main {
    public static void main(String[] args) {
        int x = -2365;
        int y = 125;
        System.out.println();
        System.out.println("Floor division using '/' operator: " + (x / y));
        System.out.println("Floor division using floorDiv() method is: " + Math.floorDiv(x, y));
        System.out.println();
        System.out.println("Floor modulus using '%' operator: " + (x % y));
        System.out.println("Floor modulus using floorMod() method is: " + Math.floorMod(x, y));
    }
}

Sample Output:

Floor division using '/' operator: -18
Floor division using floorDiv() method is: -19

Floor modulus using '%' operator: -115
Floor modulus using floorMod() method is: 10

Flowchart:

Flowchart: Java Data Type Exercises - Compute the floor division and the floor modulus of the given dividend and divisor

For more Practice: Solve these Related Problems:

  • Write a Java program to compute the floor division and modulus of two integers without using the division operator.
  • Write a Java program to compare the results of floor division and regular division for negative divisors.
  • Write a Java program to implement floor division and modulus using recursion and iterative methods.
  • Write a Java program to compute floor division and modulus for large integers using BigInteger methods.

Go to:


PREV : Compare Signed and Unsigned Numbers.
NEXT : Extract Primitive Value from BigInteger.


Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.