Sunday, June 22, 2025
HomeJavaTips on how to use Modulo , Modulus, or The rest Operator...

Tips on how to use Modulo , Modulus, or The rest Operator in Java? [Example]


Modulo Operator is likely one of the basic operators in Java. It is a binary operator i.e. it requires two operands. In a division operation, the rest is returned through the use of the modulo operator. It’s denoted by the % (share) signal. For instance, 5percent2 will return 1 as a result of should you divide 5 with 2, the rest shall be 1. For a programmer it is crucial to know learn how to use this operator, they’re crucial to construct logic. For instance, in lots of circumstances like reversing a quantity or checking if a quantity is a palindrome, you need to use the modulus operator with 10 to get the final digit, for instance, 101percent10 will return 1 or 1234percent10 will return 4, the final digit. 
It is likely one of the moderately much less used arithmetic operators compared to +, -, * and /.  One of many essential factors concerning the the rest operator which isn’t identified by many Java programmers is that it may also be used with floating-point numbers.

It is stunning since you do not usually consider actual quantity division as producing remainders. Nevertheless, there are generally when it is helpful to ask precisely what number of instances does 2.5 go into 7.5 and what’s leftover? The reply is that 2.5 goes into 7.5 thrice with zero leftovers, and it is that zero which is the results of 7.5 % 2.5 in Java.

One other good use of the modulus operator is to examine if a quantity is even or odd. Within the case of a good quantity, numberpercent2 will return 0 whereas if a quantity is odd then numberpercent2 will return 1.

What’s Modulo or The rest Operator in Java

Modulo operator is often known as The rest operator and denoted by share signal (%). It is one of the fundamental operators and really helpful to create logic in programming, obtainable in nearly each single programming language. As we be taught in first paragraph, in Java modulus, operator may also be utilized to floating-point numbers e.g. 3.0percent1.0 is completely authorized in Java. Because it return the rest worth in division operation it is usually referred to as the rest operator.

If each operands for %, the modulus operator have sort int, then operand_left %  operand_right evaluates to the integer the rest. For instance, 11 % 3 evaluates to 2 as a result of 11 divided by 3 has a the rest of two.  

Additionally if  each operands have sort int, the modulus operator (with each operands) evaluates to int. If both or each operands of the mod operator have sort double, then evaluating it produces the rest.

This sort of mod operator doesn’t exist in C or C++ the place the mod operator solely works with int operands. The evaluated result’s a double worth. For instance, 8.27 % 2 evaluates to 0.27 because the division is 4 with a the rest of 0.27. In fact, each values may also be double, so 6.7 % 2.1 evaluates to 0.4 because the division is 3 with a the rest of 0.4. 

The rest is computed by assuming the division yields an integer end result, with the remaining being a the rest

Modulo or Operator in Java

Tips on how to use Modulo Operator in Java with Instance

You can’t perceive something higher with none instance. The identical is true for the modulo operators in Java. On this instance, we will present you learn how to use the modulo operator. The modulo operator is an arithmetic operator that’s used to divide one operand by one other and return the rest as its end result.

You utilize the modulo operator to get the rest of the division between an int variable and 10 and a double variable and 10, as described within the code snippet under.

import java.util.Arrays;

/**
* Java program to display learn how to use modulo operator in Java. Modulo
* operator returns the rest of division operation between two operands. It is
* a part of Java's arithmetic operator suite.
*
* @creator Javin Paul
*/
public class Modulo{

  public static void most important(String args[]) {

  // Instance 1 - Modulo operator returns the rest
  int iValue = 101;
  double dValue = 39.02;

  System.out.println(iValue + " mod 9 = " + iValue % 9);
  System.out.println(dValue + " mod 9 = " + dValue % 9);


  // Instance 2 - module operator on 10 may give you final digit of integer quantity 
  int quantity = 215;
  int whole = 349;
  System.out.printf("Final digit of %d is %dpercentn", quantity, quantity % 10);
  System.out.printf("Final digit of %d is %dpercentn", whole, whole % 10);


  // Instance 3 - You should use modulo operator on 2 to examine if quantity is even or odd
  int even = 22;
  int odd = 21;
  System.out.printf("%d is %s numberpercentn", even, oddness(even));
  System.out.printf("%d is %s numberpercentn", odd, oddness(odd));

  }

  public static String oddness(int i) {
  return i % 2 == 0 ? "even" : "odd";
  }

}

Output :
101 mod 9 = 2
39.02 mod 9 = 3.020000000000003
Final digit of 215 is 5
Final digit of 349 is 9
22 is even quantity
21 is odd quantity

Essential factors about Modulo Operator in Java

Now we all know what’s modulus or the rest operator, let’s revisit some essential issues about this handy operator :

1) It’s also referred to as the rest operator and denoted by share signal %

2) You should use the rest operator to get the final digit of any quantity e.g. numberpercent10 gives you the final digit, a helpful trick to unravel many numeric issues.

3) modulus operator is not only relevant to integral varieties e.g. byte, quick, int, lengthy but in addition to floating-point varieties like float and double.

4) You may also use the rest operator to examine if a quantity is even or odd, or if a 12 months is bissextile year.

That is all about learn how to use the modulo operator in Java. As I stated, its one of many essential operator and may be very useful in each real-world growth in addition to fixing coding questions throughout interviews.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments