Skip to content
Advertisement

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?

Advertisement

Answer

You can use modulus operator like this in your condition,

if (x%y == 0)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement