# C/C++ Program to Find remainder of array multiplication divided by n

Given multiple numbers and a number n, the task is to print the remainder after multiply all the number divide by n.

Examples:

```Input : arr[] = {100, 10, 5, 25, 35, 14},
n = 11
Output : 9
100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive approach: First multiple all the number then take % by n then find the remainder, But in this approach, if the number is maximum of 2^64 then it give the wrong answer.

Approach that avoids overflow : First take a remainder or individual number like arr[i] % n. Then multiply the remainder with current result. After multiplication, again take remainder to avoid overflow. This works because of distributive properties of modular arithmetic. ( a * b) % c = ( ( a % c ) * ( b % c ) ) % c

 `// C++ program to find ` `// remainder when all ` `// array elements are ` `// multiplied. ` `#include ` `using` `namespace` `std; ` ` `  `// Find remainder of arr * arr * ` `// .. * arr[n-1] ` `int` `findremainder(``int` `arr[], ``int` `len, ``int` `n) ` `{ ` `    ``int` `mul = 1; ` ` `  `    ``// find the individual remainder ` `    ``// and multiple with mul. ` `    ``for` `(``int` `i = 0; i < len; i++) ` `        ``mul = (mul * (arr[i] % n)) % n; ` ` `  `    ``return` `mul % n; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 100, 10, 5, 25, 35, 14 }; ` `    ``int` `len = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``int` `n = 11; ` ` `  `    ``// print the remainder of after ` `    ``// multiple all the numbers ` `    ``cout << findremainder(arr, len, n); ` `} `

Output:

```9
```

Please refer complete article on Find remainder of array multiplication divided by n for more details!

My Personal Notes arrow_drop_up

Improved By : Akanksha_Rai

Article Tags :

Be the First to upvote.

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