Search This Blog

Monday 9 January 2012

Modulus Operator In Java


The modulus operator, %, returns the remainder of a division operation. It can be
applied to floating-point types as well as integer types. (This differs from C/C++, in
which the % can only be applied to integer types.)

The following example program demonstrates the %:

// 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);
}
}
When you run this program you will get the following output:
x mod 10 = 2
y mod 10 = 2.25

No comments:

Post a Comment