Java.math.BigInteger.modInverse() method in Java Last Updated : 17 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Prerequisite : BigInteger Basics The modInverse() method returns modular multiplicative inverse of this, mod m. This method throws an ArithmeticException if m <= 0 or this has no multiplicative inverse mod m (i.e., gcd(this, m) != 1). Syntax: public BigInteger modInverse(BigInteger m) Parameters: m - the modulus. Return Value: This method returns a BigInteger object whose value is ((this)^(-1) mod m). Exception: ArithmeticException - m <= 0, or this BigInteger has no multiplicative inverse mod m (that is, this BigInteger is not relatively prime to m). Below programs illustrate the BigInteger.modInverse() method: Program 1 : Java import java.math.*; import java.util.Scanner; public class GFG { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // create 2 BigInteger objects BigInteger biginteger1, biginteger2, result; // Initialize all BigInteger Objects biginteger1 = new BigInteger("8"); biginteger2 = new BigInteger("21"); // perform modInverse operation on biginteger1 using biginteger2. result = biginteger1.modInverse(biginteger2); String expression = biginteger1 + " ^ -1 % " + biginteger2 + " = " + result; // print result value System.out.println(expression); } } Output: 8 ^ -1 % 21 = 8 Program 2 : Java import java.math.*; import java.util.Scanner; public class GFG { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // create 2 BigInteger objects BigInteger biginteger1, biginteger2, result; // Initialize all BigInteger Objects biginteger1 = new BigInteger(88882); biginteger2 = new BigInteger(22224); // perform modInverse operation on biginteger1 using biginteger2. result = biginteger1.modInverse(biginteger2); String expression = biginteger1 + " ^ -1 % " + biginteger2 + " = " + result; // print result value System.out.println(expression); } } Output : Exception in thread "main" java.lang.ArithmeticException: BigInteger not invertible. at java.math.MutableBigInteger.modInverse(Unknown Source) at java.math.MutableBigInteger.mutableModInverse(Unknown Source) at java.math.BigInteger.modInverse(Unknown Source) at BigInteger.GFG2.main(GFG2.java:23) Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#modInverse(java.math.BigInteger) Comment More infoAdvertise with us Next Article Java.math.BigInteger.modInverse() method in Java A AmanSingh2210 Follow Improve Article Tags : Java Java - util package Java-Functions java-math Java-BigInteger Java-math-package +2 More Practice Tags : JavaJava-BigInteger Similar Reads BigInteger mod() Method in Java 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 aft 3 min read BigInteger remainder() Method in Java The java.math.BigInteger.remainder(BigInteger big) method returns a BigInteger whose value is equal to (this BigInteger % big(BigInteger passed as parameter)).The remainder operation finds the remainder after division of this BigInteger by another BigInteger passed as parameter. Syntax: public BigIn 3 min read BigInteger or() method in Java Prerequisite: BigInteger Basics The java.math.BigInteger.or(BigInteger val) method is used to perform bitwise OR of two BigIntegers. One of the BigInteger is passed in parameter and the other on which the function is called. This method returns a negative number if either of the BigIntegers used wit 2 min read BigInteger max() and min() Methods in Java Prerequisite: BigInteger Basics BigInteger max() method: The max() method of the BigInteger returns the BigInteger whose value is the greater between current BigInteger and BigInteger passed as a parameter to the method. If both the values are equal, either may be returned. There is a similar method 3 min read BigInteger xor() Method in Java Prerequisite: BigInteger Basics The xor(BigInteger val) method returns bitwise-XOR of two bigIntegers. This method returns a negative BigInteger if and only if exactly one of the current bigInteger and the bigInteger passed in parameter id negative. The xor() method of BigInteger class apply bitwise 2 min read Like