The modulus operator computes the remainder of a division operation. Let’s look at a BlueJ program to understand its usage:
public class ModulusOperator
{
public void computeModulus() {
/*
* Modulus operator computes
* the remainder of a
* division operation.
*/
int a = 59;
double b = 59.75;
System.out.println("a % 10 = " + a % 10);
System.out.println("b % 10 = " + b % 10);
}
}
Dividing 59 by 10 gives 9 as the remainder and dividing 59.75 by 10 gives 9.75 as the remainder.