How to compute mod of a big number? - GeeksforGeeks

09 Dec, 2021
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) * 10) + (y (mod a))) mod a
where, x : left-most digit
y: rest of the digits except x.
for example: 
625 % 5 = (((6 % 5)*10) + (25 % 5)) % 5 = 0
Below is the implementation.
Thanks to utkarsh111 for suggesting the below solution.
C++
Java
Python3
C#
PHP
Javascript
// C++ program to compute mod of a big number represented// as string#include <iostream>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 programint main(){    string num = "12316767678678";    cout << mod(num, 10);    return 0;}
// Java program to compute mod of a big// number represented as stringimport 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.
# 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 programnum = "12316767678678"print(mod(num, 10)) # This code is contributed by Sam007
// C# program to compute mod of a big// number represented as stringusing 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// PHP program to compute mod// of a big number represented// as string // Function to compute num (mod a)function mod($num, $a){    // Initialize result    $res = 0;     // One by one process    // all digits of 'num'    for ($i = 0; $i < $r = strlen($num); $i++)        $res = ($res * 10 +                $num[$i] - '0') % $a;     return $res;} // Driver Code$num = "12316767678678";echo mod($num, 10); // This code is contributed by ajit?>
<script> // Javascript program to compute mod// of a big number represented// as string // Function to compute num (mod a)function mod(num, a){         // Initialize result    let res = 0;     // One by one process    // all digits of 'num'    for(let i = 0; i < num.length; i++)        res = (res * 10 +            parseInt(num[i])) % a;     return res;} // Driver Codelet num = "12316767678678"; document.write(mod(num, 10)); // This code is contributed by _saurabh_jaiswal </script>
Output : 
8
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Sam007
jit_t
_saurabh_jaiswal
rs423
gaurav kataria
nehakulkarni15
large-numbers
Modular Arithmetic
Mathematical
Mathematical
Modular Arithmetic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Sieve of Eratosthenes
Print all possible combinations of r elements in a given array of size n
Program for Decimal to Binary Conversion
The Knight's tour problem | Backtracking-1
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array