Search code examples
javadivision

How do I check divisibility in Java?


I am trying to check if on number is divisible by another, and currently I use this method:

int x = 70;
int y = 30;

if(x/y == Math.round(x/y)) {
    ...
}

Is there a simpler way?


Solution

  • You can use modulus operator like this in your condition,

    if (x%y == 0)