# Program to check if a number is divisible by any of its digits

Given an integer **N** where . The task is to check whether the number is not divisible by any of its digit. If the given number N is divisible by any of its digits then print “YES” else print “NO”.

**Examples:**

Input :N = 5115Output :YESExplanation: 5115 is divisible by both 1 and 5. So print YES.Input :27Output :NOExplanation: 27 is not divisible by 2 or 7

**Approach :** The idea to solve the problem is to extract the digits of the number one by one and check if the number is divisible by any of its digit. If it is divisible by any of it’s digit then print YES otherwise print NO.

Below is the implementation of above approach:

## C++

`// CPP implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if given number is divisible ` `// by any of its digits ` `string isDivisible(` `long` `long` `int` `n) ` `{ ` ` ` `long` `long` `int` `temp = n; ` ` ` ` ` `// check if any of digit divides n ` ` ` `while` `(n) { ` ` ` `int` `k = n % 10; ` ` ` ` ` `// check if K divides N ` ` ` `if` `(temp % k == 0) ` ` ` `return` `"YES"` `; ` ` ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `return` `"NO"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `long` `long` `int` `n = 9876543; ` ` ` ` ` `cout << isDivisible(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to check if given number is divisible ` ` ` `// by any of its digits ` ` ` `static` `String isDivisible(` `int` `n) ` ` ` `{ ` ` ` `int` `temp = n; ` ` ` ` ` `// check if any of digit divides n ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `int` `k = n % ` `10` `; ` ` ` ` ` `// check if K divides N ` ` ` `if` `(temp % k == ` `0` `) ` ` ` `{ ` ` ` `return` `"YES"` `; ` ` ` `} ` ` ` `n /= ` `10` `; ` ` ` `} ` ` ` ` ` `return` `"NO"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `9876543` `; ` ` ` `System.out.println(isDivisible(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python program implementation of above approach ` ` ` `# Function to check if given number is ` `# divisible by any of its digits ` `def` `isDivisible(n): ` ` ` `temp ` `=` `n ` ` ` ` ` `# check if any of digit divides n ` ` ` `while` `(n): ` ` ` `k ` `=` `n ` `%` `10` ` ` ` ` `# check if K divides N ` ` ` `if` `(temp ` `%` `k ` `=` `=` `0` `): ` ` ` `return` `"YES"` ` ` ` ` `n ` `/` `=` `10` `; ` ` ` ` ` `# Number is not divisible by ` ` ` `# any of digits ` ` ` `return` `"NO"` ` ` `# Driver Code ` `n ` `=` `9876543` `print` `(isDivisible(n)) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to check if given number is divisible ` ` ` `// by any of its digits ` ` ` `static` `String isDivisible(` `int` `n) ` ` ` `{ ` ` ` `int` `temp = n; ` ` ` ` ` `// check if any of digit divides n ` ` ` `while` `(n > 0) ` ` ` `{ ` ` ` `int` `k = n % 10; ` ` ` ` ` `// check if K divides N ` ` ` `if` `(temp % k == 0) ` ` ` `{ ` ` ` `return` `"YES"` `; ` ` ` `} ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `return` `"NO"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `n = 9876543; ` ` ` `Console.WriteLine(isDivisible(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function to check if given number ` `// is divisible by any of its digits ` `function` `isDivisible(` `$n` `) ` `{ ` ` ` `$temp` `= ` `$n` `; ` ` ` ` ` `// check if any of digit divides n ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `$k` `= ` `$n` `% 10; ` ` ` ` ` `// check if K divides N ` ` ` `if` `(` `$temp` `% ` `$k` `== 0) ` ` ` `return` `"YES"` `; ` ` ` ` ` `$n` `= ` `floor` `(` `$n` `/ 10); ` ` ` `} ` ` ` ` ` `return` `"NO"` `; ` `} ` ` ` `// Driver Code ` `$n` `= 9876543; ` ` ` `echo` `isDivisible(` `$n` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

YES

**Time Complexity**: O(log(N))

**Auxiliary Space**: O(1)

## Recommended Posts:

- Program to check if a number is divisible by sum of its digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check whether sum of digits at odd places of a number is divisible by K
- Check if N is divisible by a number which is composed of the digits from the set {A, B}
- Check whether product of digits at even places of a number is divisible by K
- Given a large number, check if a subsequence of digits is divisible by 8
- Program to check if a given number is Lucky (all digits are different)
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find N digits number which is divisible by D
- Smallest number with sum of digits as N and divisible by 10^N
- Largest number with the given set of N digits that is divisible by 2, 3 and 5
- Number of digits to be removed to make a number divisible by 3
- Possible to make a divisible by 3 number using all digits in an array
- Check if a number is divisible by 41 or not
- Check if a number is divisible by 23 or not

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.