Java

What does % percentage sign mean in Java

There are multiple arithmetic operators in java such as +, -, /, and so on. Another useful arithmetic operator in java is % which divides the two values and consequently returns the remainder of these values. In java, there are multiple use cases of % for example it can be used to check wheater a number is even or odd, it can be used to check if the number is palindrome or not, and so on.

This write-up presents a detailed overview of the Java modulus operator (%) and in this regard following concepts will be explained:

So, let’s get started!

What does % mean in Java

The % sign means modulus and in java, it is used to get the remainder of two values, or more precisely, we can say that the % sign returns the remainder of the division operation. There are several names of % sing such as modulus, modulo, remainder.

Basic Syntax

The syntax of the modulus(%) is shown in the below-given snippet:

Number1 % Number2

Here, in the above snippet, Number1 is a dividend and Number2 is a divisor.

How to use % operator in Java

Let’s consider some examples to understand how to use % sign in java, initially, we will consider some basic examples, and gradually we will move on to some advanced examples:

Example

In this example we take two integer values and implement the modulus operator on them:

publicclassModulusExample {
publicstaticvoidmain(String[] args) {
int number1 = 13;
int number2 = 3;
System.out.println("Remainder: " + (number1 % number2));
    }  
}

In the above snippet, we take two integer values and implement the % operator on them and consequently it returns the following output:

From the output, it is clear that the % operator performs division on the given values and as a result, it returns their remainder.

Example

Let’s consider another example to test wheater the user entered number is even or odd:

publicclassModulusExample {
publicstaticvoidmain(String[] args) {
int number, result;
        Scanner scan = new Scanner(System.in);
System.out.print("Enter a number: ");
        number = scan.nextInt();
if(number % 2 == 0)
        {
System.out.println("You entered an even number");
        }
else
        {
System.out.println("You entered an odd number");
        }
    }
}

In this example, we take an input from a user and performs % operator on them is remainder is “0” then it will show “You entered an even number” otherwise it will show “You entered an odd number”:

User enter “11” consequently we get “You entered an odd number” which authenticates the working of modulus (%) operator.

Conclusion

In java, the % sign divides two numbers and consequently, it returns the remainder of these values. The % operator belongs to the category of arithmetic operators and it is very handy in performing mathematical operations such as finding if the number is even or odd, tracking the next index of the next spot in a circular array, and so on. This write-up presents a comprehensive understanding of what does % means, its basic syntax, and how to use it in java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.