Check whether a given number N is a Nude Number or not - GeeksforGeeks

03 Mar, 2021
Given an integer N, the task is to check whether N is a Nude number or not. 
A Nude number is a number that is divisible by all of its digits (which should be nonzero).  
Example:  
Input: N = 672 Output: Yes Explanation: Since, 672 is divisible by all of its three digits 6, 7 and 2. Therefore the output is Yes.Input: N = 450 Output: No Explanation: Since, 450 is not divisible by 0 (Also it gives exception). Therefore the output is No.  
 
Approach: Extract digits from number one by one and check these two conditions:  
The digit must be non-zero, to avoid exceptionAnd the digit must divide the number N
The digit must be non-zero, to avoid exception
And the digit must divide the number N
When both these condition is satisfied by all digits of N, then the number is called Nude number.Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to check if the// number if Nude number or not #include <iostream>using namespace std; // Check if all digits// of num divide numbool checkDivisbility(int num){    // array to store all digits    // of the given number    int digit;    int N = num;    while (num != 0) {        digit = num % 10;        num = num / 10;         // If any of the condition        // is true for any digit        // Then N is not a nude number        if (digit == 0 || N % digit != 0)            return false;    }     return true;} // Driver codeint main(){    int N = 128;     bool result = checkDivisbility(N);    if (result)        cout << "Yes";    else        cout << "No";     return 0;}
// Java program to check if the// number if Nude number or notimport java.util.*;class GFG{ // Check if all digits// of num divide numstatic boolean checkDivisbility(int num){    // array to store all digits    // of the given number    int digit;    int N = num;    while (num != 0)    {        digit = num % 10;        num = num / 10;         // If any of the condition        // is true for any digit        // Then N is not a nude number        if (digit == 0 || N % digit != 0)            return false;    }    return true;} // Driver codepublic static void main(String[] args){    int N = 128;     boolean result = checkDivisbility(N);    if (result)        System.out.print("Yes");    else        System.out.print("No");}} // This code is contributed by 29AjayKumar
# Python3 program to check if the# number if nude number or not # Check if all digits# of num divide numdef checkDivisbility(num):         # Array to store all digits    # of the given number    digit = 0    N = num         while (num != 0):        digit = num % 10        num = num // 10         # If any of the condition        # is true for any digit        # then N is not a nude number        if (digit == 0 or N % digit != 0):            return False     return True # Driver codeif __name__ == '__main__':         N = 128     result = checkDivisbility(N)    if (result):        print("Yes")    else:        print("No") # This code is contributed by mohit kumar 29
// C# program to check if the// number if Nude number or notusing System;class GFG{ // Check if all digits// of num divide numstatic bool checkDivisbility(int num){    // array to store all digits    // of the given number    int digit;    int N = num;    while (num != 0)    {        digit = num % 10;        num = num / 10;         // If any of the condition        // is true for any digit        // Then N is not a nude number        if (digit == 0 || N % digit != 0)            return false;    }    return true;} // Driver codepublic static void Main(){    int N = 128;     bool result = checkDivisbility(N);    if (result)        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed by Nidhi_biet
<script> // Javascript program to check if the// number if Nude number or not // Check if all digits// of num divide numfunction checkDivisbility(num){    // array to store all digits    // of the given number    let digit;    let N = num;    while (num != 0) {        digit = num % 10;        num = Math.floor(num / 10);         // If any of the condition        // is true for any digit        // Then N is not a nude number        if (digit == 0 || N % digit != 0)            return false;    }     return true;} // Driver code      let N = 128;     let result = checkDivisbility(N);    if (result)        document.write("Yes");    else        document.write("No"); // This code is contributed by Mayank Tyagi </script>
Yes
 
Time complexity: O( length of N ) 
mohit kumar 29
29AjayKumar
nidhi_biet
mayanktyagi1709
Number Divisibility
number-digits
Numbers
Mathematical
School Programming
Write From Home
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects