Modulus Operator In Java
The modulus operator in java is represented by the persent sign which is indicated as - “ % ”.
The modulus operator, %, returns the remainder of a division operation.
It can be applied to floating-point types as well as integer types.
It is also a part of the binary arithmetic operator which means that this operator is used on two operators.
The following example program demonstrates the %:
Example -
// Demonstrate the % operator.
class Modulus {
public static void main(String args[]) {
int x = 42;
double y = 42.25;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}
|
Output -
Result and remainder of 10 / 3: 3 1
Result and remainder of
10.0 / 3.0: 3.3333333333333335 1.0
|