Java Modulus Operator Example

This example demonstrates how to use a modulus operator in Java with an example.

Java Modulus Operator 

The modulus operator, %, returns the remainder of a division operation. It can be applied to floating-point types as well as integer types. 

Java Modulus Operator Example

The following example program demonstrates the %:
package net.javaguides.corejava.operators.arithmetic;

public 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:
x mod 10 = 2
y mod 10 = 2.25

Reference

Comments