Java ModuloIn this section, we will discuss the Java modulo operator. Java Modulo OperatorIn mathematics, there is basically four arithmetic operators addition (+), subtraction (), multiplication (*), and division (/). In programming, except for these four operators, there is another operator called modulo or modulus operator. It is represented by the percentage symbol (%). It is used to determine the remainder. It requires two operands. It divides the lefthand operand by the righthand operand and gives the remainder. For example: We can also use it with the assignment operator (=). For example, It takes modulus using two operands and assigns the result to left operand (a). If it appears in an expression, we must solve the expression left to right. For example, consider the following expression. If the number is completely divided, it returns gives 0 as the result. For example, 25%5 gives 0. Let's see some other examples of modulo operator. 12%2=0 12%5=2 12%5=2 12%5=3 12%5=3 Uses of Modulo Operator
For a programmer, it is very important to know how to use the modulo operator because it plays a vital role to build logic like reverse a number, find even odd, palindrome, and many more. We can also use it to find the last digit of a number. Suppose, the given number is 5678 and we have to find the last digit of the number. As we know the modulo operator determines the remainder, so we will divide the given number by 10. Therefore, we get 8 as the remainder and the last digit. We can also use the modulo operator with floatingpoint numbers. For example, Using Modulo Operator in a Java ProgramsThe following Java program demonstrates the use of the modulo operator in Java. ModuloOperatorExample1.java Output: Remainder: 1 ModuloOperatorExample2.java Output: Remainder: 0.7999999999999998 Difference Between Modulo and Division OperatorThe major difference between modulo and division operators is that the modulo (%) operator gives the remainder while the division (/) operator gives the quotient. The following Java program will clear the concept of modulo and division operator. ModuloOperatorExample3.java Output: Quotient: 6 Remainder: 1 Java Program to Find Remainder Without Using the Modulo OperatorRemainderWithoutModulo.java Output: Remainder: 6
Next TopicRepdigit Numbers in Java
