Menu
Topics Index
...
`


Operators >
Siva Nookala - 23 Aug 2016
Java has one important arithmetical operator you may not be familiar with, %, also known as the modulus operator.The modulus operator, % returns the remainder of a division operation. e.g., 15 % 4 = 3, 7 % 3 = 1, 5 % 5 = 0

How To Learn Core Java Online


As shown above, when we divide 17 (dividend) with 3 (divisor) then the quotient is 5 and the modulus (or remainder) is 2.
  • The Modulus Operator allows operation for both floating point types as well as integer types.
Find Modulus
class FindModulus
{
    public static void main(String arg[])
    {
        int x = 32;
        double y = 36.75;
        
        System.out.println("x mod 10 = " + x % 10);
        System.out.println("y mod 10 = " + y % 10);
    
    }
}
OUTPUT

x mod 10 = 2
y mod 10 = 6.75

DESCRIPTION

When 32 is divided by 10, the quotient will be 3 and the modulus (or remainder) will be 2. Similarly when 36.75 is divided by 10, the quotient will again be 3, where as the modulus will be 6.75.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.