Java Remainder of Integer Divison?

    public static void main(String[] args) {
        int dividend = 139, divisor = 7;

        int quotient = dividend / divisor;
        int remainder = dividend % divisor;

        System.out.println("The Quotient is = " + quotient);
        System.out.println("The Remainder is = " + remainder);
    }

Output:

The Quotient is = 19

The Remainder is = 6


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);

Tags:

Java

Math

Divide