There is a math function called modulus that will do the work for you.

The Remainder or Modulus Operator in Java