# How to compute mod of a big number?

Given a big number ‘num’ represented as string and an integer x, find value of “num % x” or “num mod x”. Output is expected as an integer.

Examples :

```Input:  num = "12316767678678",  a = 10
Output: num (mod a) ≡ 8
```

The idea is to process all digits one by one and use the property that xy (mod a) ≡ ((x (mod a) * y) (mod a)). Below is the implementation.

Thanks to utkarsh111 for suggesting the below solution.

## C++

 `// C++ program to compute mod of a big number represented ` `// as string ` `#include ` `using` `namespace` `std; ` ` `  `// Function to compute num (mod a) ` `int` `mod(string num, ``int` `a) ` `{ ` `    ``// Initialize result ` `    ``int` `res = 0; ` ` `  `    ``// One by one process all digits of 'num' ` `    ``for` `(``int` `i = 0; i < num.length(); i++) ` `         ``res = (res*10 + (``int``)num[i] - ``'0'``) %a; ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``string num = ``"12316767678678"``; ` `    ``cout << mod(num, 10); ` `    ``return` `0; ` `} `

## Java

 `// Java program to compute mod of a big  ` `// number represented as string ` `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``// Function to compute num (mod a) ` `    ``static` `int` `mod(String num, ``int` `a) ` `    ``{ ` `         `  `        ``// Initialize result ` `        ``int` `res = ``0``; ` ` `  `        ``// One by one process all digits of 'num' ` `        ``for` `(``int` `i = ``0``; i < num.length(); i++) ` `            ``res = (res * ``10` `+ (``int``)num.charAt(i) ` `                                     ``- ``'0'``) % a; ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver program ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `         `  `        ``String num = ``"12316767678678"``; ` `         `  `        ``System.out.println(mod(num, ``10``)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python3

 `# program to compute mod of a big number ` `# represented as string ` ` `  `# Function to compute num (mod a) ` `def` `mod(num, a): ` `     `  `    ``# Initialize result ` `    ``res ``=` `0` ` `  `    ``# One by one process all digits ` `    ``# of 'num' ` `    ``for` `i ``in` `range``(``0``, ``len``(num)): ` `        ``res ``=` `(res ``*` `10` `+` `int``(num[i])) ``%` `a; ` ` `  `    ``return` `res ` ` `  `# Driver program ` `num ``=` `"12316767678678"``; ` `print``(mod(num, ``10``)) ` ` `  `# This code is contributed by Sam007 `

## C#

 `// C# program to compute mod of a big  ` `// number represented as string ` `using` `System; ` ` `  `public` `class` `GFG  ` `{ ` `     `  `    ``// Function to compute num (mod a) ` `    ``static` `int` `mod(String num, ``int` `a) ` `    ``{ ` `         `  `        ``// Initialize result ` `        ``int` `res = 0; ` ` `  `        ``// One by one process all  ` `        ``// digits of 'num' ` `        ``for` `(``int` `i = 0; i < num.Length; i++) ` `            ``res = (res * 10 + (``int``)num[i]-  ` `                   ``'0'``) % a; ` ` `  `        ``return` `res; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``String num = ``"12316767678678"``; ` `         `  `        ``Console.WriteLine(mod(num, 10)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` `

Output :

`8`

code