How can You Calculate Remainder in Java?

Tech

Java has a significant arithmetical operator which all people aren’t familiar with. This is known as modulus in Java or the Remainder Operator. The % operator gives you the remainder of two figures. For example: 10 % 3 is 1 as when 10 is divided by 3, it holds a remainder of 1. You can easily make use of %, just like you use the other operators like – or +.

The modulus operator gives you the remainder of a division calculated. It can be applied to an integer type or to floating point type. It is different from C or C++ where the modulus operator can only be used on integer types.

If you look at the program of modulus in Java operator, it is somewhat like this:

class Remainder {

    public static void main (String args []) {

        int i = 10;
        int j = 9;

        System.out.println (“i is” +i);
        System.out.println (“j is” +j);

        int k = i % j;

        System.outprintln ( “i%j is” +k);
    } 
}

And, the output is:

% javac Remainder. java

% java Remainder

i is 10

J is 9

i % j is 1

In Java, suppose the question is:

a = a+4;

well, you can rewrite it as a +=4;

Similar is the case with the modulus in Java;

For a = a %2

You can write- a%= 2;

The assignment operators help allow the programmers to save a bit on typing because of the shorthand for their long forms. It also allows better implementation of the operators by the java run- time.

The modulus in Java helps in quick division of the arithmetic numbers. So, if you are looking for some long and tough programming, you can also use the shorthand typing to reduce your burden. Use the modulus operation for all the calculations you need!