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

• Last Updated : 23 Jun, 2022

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 = 5115
Output : YES
Explanation: 5115 is divisible by both 1 and 5.
So print YES.

Input : 27
Output : NO
Explanation: 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++

 // C++ implementation of above approach#include using namespace std; // Function to check if given number is divisible// by any of its digitsstring 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 Codeint main(){    long long int n = 9876543;     cout << isDivisible(n);     return 0;}

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

## Python3

 # Python program implementation of above approach # Function to check if given number is# divisible by any of its digitsdef 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 Coden = 9876543print(isDivisible(n)) # This code is contributed by# Sanjit_Prasad

## C#

 // C# implementation of above approachusing 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

## PHP

 

## Javascript

 

Output:

YES

Time Complexity: O(log(N))
Auxiliary Space: O(1), since no extra space has been required.

#### Method #2: Using string:

• We have to convert the given number to string by taking a new variable .
• Traverse the string ,
• Convert character to integer(digit)
• Check if the number is divisible by any of it’s digit then print YES otherwise print NO.

Below is the implementation of above approach:

## C++

 //C++ implementation of above approach#include using namespace std;  string getResult(int n) {       // Converting integer to string    string st = to_string(n);         // Traversing the string    for (int i = 0; i < st.length(); i++)    {               //find the actual digit        int d = st[i] - 48;              // If the number is divisible by        // digits then return yes        if(n % d == 0)        {                       return "Yes";        }    }       // If no digits are dividing the    // number then return no    return "No";} // Driver Codeint main(){int n = 9876543; // passing this number to get result functioncout<

## Java

 // JAva implementation of above approachimport java.io.*; class GFG{static String getResult(int n) {       // Converting integer to string    String st = Integer.toString(n);         // Traversing the string    for (int i = 0; i < st.length(); i++)    {               //find the actual digit        int d = st.charAt(i) - 48;              // If the number is divisible by        // digits then return yes        if(n % d == 0)        {                       return "Yes";        }    }       // If no digits are dividing the    // number then return no    return "No";} // Driver Codepublic static void main(String[] args)    {int n = 9876543; // passing this number to get result functionSystem.out.println(getResult(n));}} // this code is contributed by shivanisinghss2110

## Python3

 # Python implementation of above approachdef getResult(n):       # Converting integer to string    st = str(n)         # Traversing the string    for i in st:               # If the number is divisible by        # digits then return yes        if(n % int(i) == 0):            return 'Yes'               # If no digits are dividing the    # number then return no    return 'No'  # Driver Coden = 9876543 # passing this number to get result functionprint(getResult(n))# this code is contributed by vikkycirus

## C#

 // C# implementation of above approachusing System; public class GFG{static String getResult(int n) {       // Converting integer to string    string st = n.ToString();         // Traversing the string    for (int i = 0; i < st.Length; i++)    {               //find the actual digit        int d = st[i] - 48;              // If the number is divisible by        // digits then return yes        if(n % d == 0)        {                       return "Yes";        }    }       // If no digits are dividing the    // number then return no    return "No";} // Driver Codepublic static void Main(String[] args){   int n = 9876543;    // passing this number to get result function   Console.Write(getResult(n));}} // this code is contributed by shivanisinghss2110

## Javascript

 

#### Output:

Yes`

Time Complexity: O(n)

Auxiliary Space: O(n)

My Personal Notes arrow_drop_up