# Modular multiplicative inverse

Here given code implementation process.

``````// C Program
// Modular multiplicative inverse
#include <stdio.h>

// This function find the inverse of a (a^-1) under m
void invert_modulo(int a, int m)
{
int num = a % m;
//Loop controlling variable
int i = 1;
//result variable
int result = -1;
//Display given data information
printf("\n Number  [a] : %d", a);
printf("\n Modular [m] : %d", m);
while (i < m && result == -1)
{
if ((num * i) % m == 1)
{
//When get result
result = i;
}
i++;
}
if (result == -1)
{
//When result are not possible
}
else
{
printf("\n Result %d\n", result);
}
}
int main()
{
printf("\n Test Modular multiplicative inverse\n");
//Test function
invert_modulo(7, 34);
invert_modulo(9, 28);
invert_modulo(28, 9);
return 0;
}``````

#### Output

`````` Test Modular multiplicative inverse

Number  [a] : 7
Modular [m] : 34
Result 5

Number  [a] : 9
Modular [m] : 28
Result 25

Number  [a] : 28
Modular [m] : 9
Result 1``````
``````// Java program
// Modular multiplicative inverse
class ModularInverse
{
// This function find the inverse of a (a^-1) under m
public void invert_modulo(int a, int m)
{
int num = a % m;
//Loop controlling variable
int i = 1;
//result variable
int result = -1;
//Display given data information
System.out.print("\n Number [a] : " + a);
System.out.print("\n Modular [m] : " + m);
while (i < m && result == -1)
{
if ((num * i) % m == 1)
{
//When get result
result = i;
}
i++;
}
if (result == -1)
{
//When result are not possible
}
else
{
System.out.print("\n Result " + result + "\n");
}
}
public static void main(String[] args)
{
ModularInverse obj = new ModularInverse();
System.out.print("\n Test Modular multiplicative inverse\n");
// Test Case
obj.invert_modulo(7, 34);
obj.invert_modulo(9, 28);
obj.invert_modulo(28, 9);
}
}``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ program
// Modular multiplicative inverse
class ModularInverse
{
public:
// This function find the inverse of a (a^-1) under m
void invert_modulo(int a, int m)
{
int num = a % m;
//Loop controlling variable
int i = 1;
//result variable
int result = -1;
//Display given data information
cout << "\n Number [a] : " << a;
cout << "\n Modular [m] : " << m;
while (i < m && result == -1)
{
if ((num * i) % m == 1)
{
//When get result
result = i;
}
i++;
}
if (result == -1)
{
//When result are not possible
}
else
{
cout << "\n Result " << result << "\n";
}
}
};
int main()
{
ModularInverse obj = ModularInverse();
cout << "\n Test Modular multiplicative inverse\n";
// Test Case
obj.invert_modulo(7, 34);
obj.invert_modulo(9, 28);
obj.invert_modulo(28, 9);
return 0;
}``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1``````
``````//Include namespace system
using System;

// C# program
// Modular multiplicative inverse

class ModularInverse
{
// This function find the inverse of a (a^-1) under m
public void invert_modulo(int a, int m)
{
int num = a % m;
//Loop controlling variable
int i = 1;
//result variable
int result = -1;
//Display given data information
Console.Write("\n Number [a] : " + a);
Console.Write("\n Modular [m] : " + m);
while (i < m && result == -1)
{
if ((num * i) % m == 1)
{
//When get result
result = i;
}
i++;
}
if (result == -1)
{
//When result are not possible
}
else
{
Console.Write("\n Result " + result + "\n");
}
}
public static void Main(String[] args)
{
ModularInverse obj = new ModularInverse();
Console.Write("\n Test Modular multiplicative inverse\n");
// Test Case
obj.invert_modulo(7, 34);
obj.invert_modulo(9, 28);
obj.invert_modulo(28, 9);
}
}``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1``````
``````<?php
// Php program
// Modular multiplicative inverse
class ModularInverse
{
// This function find the inverse of a (a^-1) under m
public	function invert_modulo(\$a, \$m)
{
\$num = \$a % \$m;
//Loop controlling variable
\$i = 1;
//result variable
\$result = -1;
//Display given data information
echo "\n Number [a] : ". \$a;
echo "\n Modular [m] : ". \$m;
while (\$i < \$m && \$result == -1)
{
if ((\$num * \$i) % \$m == 1)
{
//When get result
\$result = \$i;
}
\$i++;
}
if (\$result == -1)
{
//When result are not possible
}
else
{
echo "\n Result ". \$result ."\n";
}
}
}

function main()
{
\$obj = new ModularInverse();
echo "\n Test Modular multiplicative inverse\n";
// Test Case
\$obj->invert_modulo(7, 34);
\$obj->invert_modulo(9, 28);
\$obj->invert_modulo(28, 9);
}
main();``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1``````
``````// Node Js program
// Modular multiplicative inverse
class ModularInverse
{
// This function find the inverse of a (a^-1) under m
invert_modulo(a, m)
{
var num = a % m;
//Loop controlling variable
var i = 1;
//result variable
var result = -1;
//Display given data information
process.stdout.write("\n Number [a] : " + a);
process.stdout.write("\n Modular [m] : " + m);
while (i < m && result == -1)
{
if ((num * i) % m == 1)
{
//When get result
result = i;
}
i++;
}
if (result == -1)
{
//When result are not possible
}
else
{
process.stdout.write("\n Result " + result + "\n");
}
}
}

function main()
{
var obj = new ModularInverse();
process.stdout.write("\n Test Modular multiplicative inverse\n");
// Test Case
obj.invert_modulo(7, 34);
obj.invert_modulo(9, 28);
obj.invert_modulo(28, 9);
}
main();``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1``````
``````#  Python 3 program
#  Modular multiplicative inverse
class ModularInverse :
#  This function find the inverse of a (a^-1) under m
def invert_modulo(self, a, m) :
num = a % m
# Loop controlling variable
i = 1
# result variable
result = -1
# Display given data information
print("\n Number [a] : ", a, end = "")
print("\n Modular [m] : ", m, end = "")
while (i < m and result == -1) :
if ((num * i) % m == 1) :
# When get result
result = i

i += 1

if (result == -1) :
# When result are not possible
else :
print("\n Result ", result ,"\n", end = "")

def main() :
obj = ModularInverse()
print("\n Test Modular multiplicative inverse\n", end = "")
#  Test Case
obj.invert_modulo(7, 34)
obj.invert_modulo(9, 28)
obj.invert_modulo(28, 9)

if __name__ == "__main__": main()``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] :  7
Modular [m] :  34
Result  5

Number [a] :  9
Modular [m] :  28
Result  25

Number [a] :  28
Modular [m] :  9
Result  1``````
``````#  Ruby program
#  Modular multiplicative inverse
class ModularInverse

#  This function find the inverse of a (a^-1) under m
def invert_modulo(a, m)

num = a % m
# Loop controlling variable
i = 1
# result variable
result = -1
# Display given data information
print("\n Number [a] : ", a)
print("\n Modular [m] : ", m)
while (i < m && result == -1)

if ((num * i) % m == 1)

# When get result
result = i
end
i += 1
end
if (result == -1)

# When result are not possible
else

print("\n Result ", result ,"\n")
end
end
end
def main()

obj = ModularInverse.new()
print("\n Test Modular multiplicative inverse\n")
#  Test Case
obj.invert_modulo(7, 34)
obj.invert_modulo(9, 28)
obj.invert_modulo(28, 9)
end
main()``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1
``````
``````// Scala program
// Modular multiplicative inverse
class ModularInverse
{
// This function find the inverse of a (a^-1) under m
def invert_modulo(a: Int, m: Int): Unit = {
var num: Int = a % m;
//Loop controlling variable
var i: Int = 1;
//result variable
var result: Int = -1;
//Display given data information
print("\n Number [a] : " + a);
print("\n Modular [m] : " + m);
while (i < m && result == -1)
{
if ((num * i) % m == 1)
{
//When get result
result = i;
}
i += 1;
}
if (result == -1)
{
//When result are not possible
}
else
{
print("\n Result " + result + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: ModularInverse = new ModularInverse();
print("\n Test Modular multiplicative inverse\n");
// Test Case
obj.invert_modulo(7, 34);
obj.invert_modulo(9, 28);
obj.invert_modulo(28, 9);
}
}``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1``````
``````// Swift program
// Modular multiplicative inverse
class ModularInverse
{
// This function find the inverse of a (a^-1) under m
func invert_modulo(_ a: Int, _ m: Int)
{
let num: Int = a % m;
//Loop controlling variable
var i: Int = 1;
//result variable
var result: Int = -1;
//Display given data information
print("\n Number [a] : ", a, terminator: "");
print("\n Modular [m] : ", m, terminator: "");
while (i < m && result == -1)
{
if ((num * i) % m == 1)
{
//When get result
result = i;
}
i += 1;
}
if (result == -1)
{
//When result are not possible
}
else
{
print("\n Result ", result ,"\n", terminator: "");
}
}
}
func main()
{
let obj: ModularInverse = ModularInverse();
print("\n Test Modular multiplicative inverse\n", terminator: "");
// Test Case
obj.invert_modulo(7, 34);
obj.invert_modulo(9, 28);
obj.invert_modulo(28, 9);
}
main();``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] :  7
Modular [m] :  34
Result  5

Number [a] :  9
Modular [m] :  28
Result  25

Number [a] :  28
Modular [m] :  9
Result  1``````
``````fn main()
{
print!("\n Test Modular multiplicative inverse\n");
//Test function
invert_modulo(7, 34);
invert_modulo(9, 28);
invert_modulo(28, 9);

}
fn invert_modulo(a: i32, m: i32)
{
let num: i32 = a % m;
//Loop controlling variable
let mut i: i32 = 1;
//result variable
let mut result: i32 = -1;
//Display given data information
print!("\n Number [a] : {}", a);
print!("\n Modular [m] : {}", m);
while i < m && result == -1
{
if (num * i) % m == 1
{
//When get result
result = i;
}
i += 1;
}
if result == -1
{
//When result are not possible
}
else
{
print!("\n Result {}\n", result);
}
}``````

#### Output

`````` Test Modular multiplicative inverse

Number [a] : 7
Modular [m] : 34
Result 5

Number [a] : 9
Modular [m] : 28
Result 25

Number [a] : 28
Modular [m] : 9
Result 1``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.