Open In App

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 = 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++ 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;
}

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

                    
# 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

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

                    
<script>
 
// Javascript 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 = Math.floor(n / 10);
    }
 
    return "NO";
}
 
// Driver Code
let n = 9876543;
 
document.write(isDivisible(n));
 
// This code is contributed by sravan kumar
 
</script>

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

                    

Output
YES




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

Method #2: Using string:

Below is the implementation of above approach:

//C++ implementation of above approach
#include <bits/stdc++.h>
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 Code
int main()
{
int n = 9876543;
 
// passing this number to get result function
cout<<getResult(n);
 
}
 
// this code is contributed by SoumiikMondal

                    
// JAva implementation of above approach
import 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 Code
public static void main(String[] args)
    {
int n = 9876543;
 
// passing this number to get result function
System.out.println(getResult(n));
}
}
 
// this code is contributed by shivanisinghss2110

                    
# Python implementation of above approach
def 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 Code
n = 9876543
 
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus

                    
// C# implementation of above approach
using 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 Code
public 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

                    
<script>
 
// JavaScript implementation of above approach
 
function getResult(n)
{
    // Converting integer to string
    let st = n.toString();
      
    // Traversing the string
    for (let i = 0; i < st.length; i++)
    {
        
        //find the actual digit
        let d = st[i].charCodeAt(0) - 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 Code
let n = 9876543;
  
// passing this number to get result function
document.write(getResult(n));
 
// This code is contributed by unknown2108
 
</script>

                    

Output:

Yes

Time Complexity: O(n)

Auxiliary Space: O(n)

Approach 3: Stack:

In this approach, we use a stack to store the digits of the given number. 

Here is the code 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)
{
    stack<int> digits;
 
    // push all the digits onto the stack
    while (n) {
        digits.push(n % 10);
        n /= 10;
    }
 
    // check if any of digit divides n
    while (!digits.empty()) {
        int k = digits.top();
        digits.pop();
 
        // check if K divides N
        if (n % k == 0)
            return "YES";
    }
 
    return "NO";
}
 
// Driver Code
int main()
{
    long long int n = 9876543;
 
    cout << isDivisible(n);
 
    return 0;
}

                    
import java.util.*;
 
class GFG {
    // Function to check if the given number is divisible by any of its digits
    static String isDivisible(long n) {
        Stack<Integer> digits = new Stack<>();
 
        // Push all the digits onto the stack
        while (n != 0) {
            digits.push((int)(n % 10));
            n /= 10;
        }
 
        // Check if any of the digits divides n
        while (!digits.empty()) {
            int k = digits.pop();
 
            // Check if k divides n
            if (n % k == 0)
                return "YES";
        }
 
        return "NO";
    }
 
    // Driver code
    public static void main(String[] args) {
        long n = 9876543;
 
        System.out.println(isDivisible(n));
    }
}

                    
def isDivisible(n):
    digits = []  # push all the digits onto the stack
    while n:
        digits.append(n % 10)
        n //= 10
    # check if any of digit divides n
    for k in reversed(digits):
        if k != 0 and n % k == 0# check if K divides N
            return "YES"
    return "NO"
 
 
n = 9876543
print(isDivisible(n))  # Driver Code

                    
using System;
using System.Collections.Generic;
 
public class GFG {
    // Function to check if given number is divisible
    // by any of its digits
    public static string IsDivisible(long n)
    {
        Stack<int> digits = new Stack<int>();
 
        // push all the digits onto the stack
        while (n != 0) {
            digits.Push((int)(n % 10));
            n /= 10;
        }
 
        // check if any of digit divides n
        while (digits.Count > 0) {
            int k = digits.Pop();
 
            // check if K divides N
            if (n % k == 0)
                return "YES";
        }
 
        return "NO";
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        long n = 9876543;
 
        Console.WriteLine(IsDivisible(n));
    }
}

                    
// Function to check if given number is divisible
// by any of its digits
function isDivisible(n) {
    let digits = [];
 
    // push all the digits onto the stack
    while (n) {
        digits.push(n % 10);
        n = Math.floor(n / 10);
    }
 
    // check if any of digit divides n
    while (digits.length > 0) {
        let k = digits.pop();
 
        // check if K divides N
        if (n % k == 0)
            return "YES";
    }
 
    return "NO";
}
 
// Driver Code
let n = 9876543;
console.log(isDivisible(n));

                    

Output: 

YES

Time Complexity: O(log(N)) 
Auxiliary Space: O(log n), where n is the given number. 


Article Tags :