# Check whether a large number represented as array is divisible by Y

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

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

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 ` `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; ` `} `

## 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  `

## 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 `

## 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  `

Output:

```Yes
```

My Personal Notes arrow_drop_up

Competitive Programmer, Full Stack Developer, Technical Content Writer, Machine Learner

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.

Improved By : ihritik, sanjeev2552

Article Tags :
Practice Tags :

Be the First to upvote.

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