# Java.math.BigInteger.modInverse() method in Java

Prerequisite : BigInteger Basics

The modPow() 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 :

 `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 :

 `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)
```

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.