Given a large integer **X** represented as an array **arr[]** where each **arr[i]** stores a digit in **X**. The task is to check whether the number represented by the array is divisible by given integer **Y**.

**Examples:**

Input:arr[] = {1, 2, 1, 5, 6}, Y = 4

Output:Yes

12156 / 4 = 3039

Input:arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1}, Y = 14

Output:No

**Approach:** Start traversing the digits of the given number from the left and take the largest number which is smaller than or equal to **Y** and divide it with **Y**. If the remainder is something other than **0** then it will be carried to the next possible number formed from the remaining digits just like in the long division. After the complete number is processed, if the remainder is still something other than 0 then the represented number is not divisible by **Y** else it is.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function that returns true if the number represented ` `// by the given array is divisible by y ` `bool` `isDivisible(` `int` `* arr, ` `int` `n, ` `int` `y) ` `{ ` ` ` `int` `d = 0, i = 0; ` ` ` ` ` `// While there are digits left ` ` ` `while` `(i < n) { ` ` ` ` ` `// Select the next part of the number ` ` ` `// i.e. the maximum number which is <= y ` ` ` `while` `(d < y && i < n) ` ` ` `d = d * 10 + arr[i++]; ` ` ` ` ` `// Get the current remainder ` ` ` `d = d % y; ` ` ` `} ` ` ` ` ` `// If the final remainder is 0 ` ` ` `if` `(d == 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 2, 1, 5, 6 }; ` ` ` `int` `x = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `int` `y = 4; ` ` ` ` ` `cout << (isDivisible(arr, x, y) ? ` `"Yes"` `: ` `"No"` `); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function that returns true if the number represented ` ` ` `// by the given array is divisible by y ` ` ` `static` `boolean` `isDivisible(` `int` `[] arr, ` `int` `n, ` `int` `y) ` ` ` `{ ` ` ` `int` `d = ` `0` `, i = ` `0` `; ` ` ` ` ` `// While there are digits left ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` ` ` `// Select the next part of the number ` ` ` `// i.e. the maximum number which is <= y ` ` ` `while` `(d < y && i < n) ` ` ` `d = d * ` `10` `+ arr[i++]; ` ` ` ` ` `// Get the current remainder ` ` ` `d = d % y; ` ` ` `} ` ` ` ` ` `// If the final remainder is 0 ` ` ` `if` `(d == ` `0` `) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `int` `[] arr = { ` `1` `, ` `2` `, ` `1` `, ` `5` `, ` `6` `}; ` ` ` `int` `x = arr.length; ` ` ` `int` `y = ` `4` `; ` ` ` ` ` `System.out.println(isDivisible(arr, x, y) ? ` `"Yes"` `: ` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function that returns true if the number represented ` `# by the given array is divisible by y ` `def` `isDivisible(arr, n, y): ` ` ` `d, i ` `=` `0` `, ` `0` ` ` ` ` `# While there are digits left ` ` ` `while` `i < n: ` ` ` ` ` `# Select the next part of the number ` ` ` `# i.e. the maximum number which is <= y ` ` ` `while` `d < y ` `and` `i < n: ` ` ` `d ` `=` `d ` `*` `10` `+` `arr[i] ` ` ` `i ` `+` `=` `1` ` ` ` ` `# Get the current remainder ` ` ` `d ` `=` `d ` `%` `y ` ` ` ` ` `# If the final remainder is 0 ` ` ` `if` `d ` `=` `=` `0` `: ` ` ` `return` `True` ` ` `return` `False` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `1` `, ` `5` `, ` `6` `] ` ` ` `x ` `=` `len` `(arr) ` ` ` `y ` `=` `4` ` ` `if` `(isDivisible(arr, x, y)): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by ` `# sanjeev2552 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function that returns true if the number represented ` ` ` `// by the given array is divisible by y ` ` ` `static` `bool` `isDivisible(` `int` `[] arr, ` `int` `n, ` `int` `y) ` ` ` `{ ` ` ` `int` `d = 0, i = 0; ` ` ` ` ` `// While there are digits left ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` ` ` `// Select the next part of the number ` ` ` `// i.e. the maximum number which is <= y ` ` ` `while` `(d < y && i < n) ` ` ` `d = d * 10 + arr[i++]; ` ` ` ` ` `// Get the current remainder ` ` ` `d = d % y; ` ` ` `} ` ` ` ` ` `// If the final remainder is 0 ` ` ` `if` `(d == 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` ` ` `int` `[] arr = { 1, 2, 1, 5, 6 }; ` ` ` `int` `x = arr.Length; ` ` ` `int` `y = 4; ` ` ` ` ` `Console.WriteLine(isDivisible(arr, x, y) ? ` `"Yes"` `: ` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by ihritik ` |

*chevron_right*

*filter_none*

**Output:**

Yes

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:

- Square of large number represented as String
- Check if any permutation of a large number is divisible by 8
- Check in binary array the number represented by a subarray is odd or even
- Check whether the given decoded string is divisible by 6
- Count array elements that can be represented as sum of at least two consecutive array elements
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Add 1 to number represented as array | Recursive Approach
- Sum of two numbers where one number is represented as array of digits
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Check whether a given array is a k sorted array or not
- Check whether an Array is Subarray of another Array
- Check whether an array of strings can correspond to a particular number X
- Check whether a given number N is a Nude Number or not
- Find smallest possible Number from a given large Number with same count of digits
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Minimum cost to connect weighted nodes represented as array
- Count elements in an Array that can be represented as difference of two perfect squares
- Highest power of 2 that divides a number represented in binary
- Find the smallest positive number which can not be represented by given digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.