# BigInteger mod() Method in Java

• Difficulty Level : Basic
• Last Updated : 04 Dec, 2018

The java.math.BigInteger.mod(BigInteger big) method returns a BigInteger whose value is equal to (this BigInteger modfunction big(BigInteger passed as parameter)).In other words we can say that this method returns the value after performing (this % big) step.The mod operation finds the remainder after division of this BigInteger by another BigInteger passed as parameter.

java.math.BigInteger.mod(BigInteger big) and java.math.BigInteger.remainder(BigInteger val) both function finds remainder but mod operation always returns a non-negative BigInteger.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Syntax:

`public BigInteger mod(BigInteger big)`

Parameter: The function accepts a single mandatory parameter big which specifies the BigInteger Object by which we want to divide this bigInteger object.

Return Value: The method returns the BigInteger which is equal to this BigInteger mod big(BigInteger passed as parameter).

Exception: The method throws ArithmeticException when big ≤ 0

Examples:

```Input: BigInteger1=321456, BigInteger2=31711
Output: 4346
Explanation: BigInteger1.mod(BigInteger2)=4346. The divide operation between
321456 and 31711 returns 4346 as remainder.

Input: BigInteger1=59185482345, BigInteger2=59185482345
Output: 0
Explanation: BigInteger1.mod(BigInteger2)=0. The divide operation between
59185482345 and 59185482345 returns 0 as remainder.
```

Below programs illustrate mod() method of BigInteger class:

Example 1:

 `// Java program to demonstrate mod() method of BigInteger`` ` `import` `java.math.BigInteger;`` ` `public` `class` `GFG {`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating 2 BigInteger objects``        ``BigInteger b1, b2;`` ` `        ``b1 = ``new` `BigInteger(``"321456"``);``        ``b2 = ``new` `BigInteger(``"31711"``);`` ` `        ``// apply mod() method``        ``BigInteger result = b1.mod(b2);`` ` `        ``// print result``        ``System.out.println("Result of mod ``                 ``operation between " + b1``                 ``+ ``" and "` `+ b2 +``                 ``" equal to "` `+ result);``    ``}``}`
Output:
```Result of mod operation between 321456 and 31711 equal to 4346
```

Example 2: When both are equal in value.

 `// Java program to demonstrate mod() method of BigInteger`` ` `import` `java.math.BigInteger;`` ` `public` `class` `GFG {`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating 2 BigInteger objects``        ``BigInteger b1, b2;`` ` `        ``b1 = ``new` `BigInteger(``"3515219485"``);``        ``b2 = ``new` `BigInteger(``"3515219485"``);`` ` `        ``// apply mod() method``        ``BigInteger result = b1.mod(b2);`` ` `        ``// print result``        ``System.out.println("Result of mod ``                 ``operation between " + b1``                 ``+ ``" and "` `+ b2 +``                 ``" equal to "` `+ result);``    ``}``}`
Output:
```Result of mod operation between 3515219485 and 3515219485 equal to 0
```

Example 3: Program showing exception when BigInteger passed as a parameter is less than zero.

 `// Java program to demonstrate mod() method of BigInteger`` ` `import` `java.math.BigInteger;`` ` `public` `class` `GFG {`` ` `    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``// Creating 2 BigInteger objects``        ``BigInteger b1, b2;`` ` `        ``b1 = ``new` `BigInteger(``"3515219485"``);``        ``b2 = ``new` `BigInteger(``"-1711"``);`` ` `        ``// apply mod() method``        ``BigInteger result = b1.mod(b2);`` ` `        ``// print result``        ``System.out.println("Result of mod ``                 ``operation between " + b1``                 ``+ ``" and "` `+ b2 +``                 ``" equal to "` `+ result);``    ``}``}`

Output:

```Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus not positive
at java.math.BigInteger.mod(BigInteger.java:2458)
at GFG.main(GFG.java:17)
```

My Personal Notes arrow_drop_up