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<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 program int main() { string num = "12316767678678"; cout << mod(num, 10); return 0; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
PHP
<?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 ?> |
chevron_right
filter_none
Output :
8
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count number of solutions of x^2 = 1 (mod p) in given range
- Program for quotient and remainder of big number
- Prime factors of a big number
- Divide a big number into two parts that differ by k
- Find power of power under mod of a prime
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- Find value of y mod (2 raised to power x)
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Trick for modular division ( (x1 * x2 .... xn) / b ) mod (m)
- Divide two integers without using multiplication, division and mod operator | Set2
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Find (1^n + 2^n + 3^n + 4^n) mod 5 | Set 2
- Sorting Big Integers
- Compute the parity of a number using XOR and table look-up
- Compute n! under modulo p
- Compute sum of digits in all numbers from 1 to n
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Compute (a*b)%c such that (a%c) * (b%c) can be beyond range

