# Check if all digits of a number divide it

Given a number n, find whether all digits of n divide it or not.

Examples:

```Input : 128
Output : Yes
128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.

Input : 130
Output : No
```

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

We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128 % d == 0 for d = 1, 2, 8. To do that, we need to iterate over each digit of the number.

## CPP

 `// CPP program to check the number ` `// is divisible by all digits are not. ` `#include ` `using` `namespace` `std; ` ` `  `// Function to check the divisibility ` `// of the number by its digit. ` `bool` `checkDivisibility(``int` `n, ``int` `digit) ` `{ ` `    ``// If the digit divides the number ` `    ``// then return true else return false. ` `    ``return` `(digit != 0 && n % digit == 0); ` `} ` ` `  `// Function to check if all digits ` `// of n divide it or not ` `bool` `allDigitsDivide(``int` `n) ` `{ ` `    ``int` `temp = n; ` `    ``while` `(temp > 0) { ` ` `  `        ``// Taking the digit of the ` `        ``// number into digit var. ` `        ``int` `digit = n % 10; ` `        ``if` `(!(checkDivisibility(n, digit))) ` `            ``return` `false``; ` ` `  `        ``temp /= 10; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver function ` `int` `main() ` `{ ` `    ``int` `n = 128; ` `    ``if` `(allDigitsDivide(n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to check whether ` `// number is divisible by all its digits. ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to check the divisibility ` `    ``// of the number by its digit. ` `    ``static` `boolean` `checkDivisibility(``int` `n, ``int` `digit) ` `    ``{ ` `        ``// If the digit divides the number ` `        ``// then return  true else return false. ` `        ``return` `(digit != ``0` `&& n % digit == ``0``); ` `    ``} ` ` `  `    ``// Function to check if all ` `    ``// digits of n divide it or not, ` `    ``static` `boolean` `allDigitsDivide(``int` `n) ` `    ``{ ` `        ``int` `temp = n; ` `        ``while` `(temp > ``0``) { ` ` `  `            ``// Taking the digit of the ` `            ``// number into var 'digit'. ` `            ``int` `digit = n % ``10``; ` ` `  `            ``if` `((checkDivisibility(n, digit)) == ``false``) ` `                ``return` `false``; ` ` `  `            ``temp /= ``10``; ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver function ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``128``; ` ` `  `        ``// funtion call to check ` `        ``// digits divisibility ` `        ``if` `(allDigitsDivide(n)) ` `            ``System.out.println(``"Yes"``); ` ` `  `        ``else` `            ``System.out.println(``"No"``); ` `    ``} ` `} ` ` `  `/*This code is contributed by Nikita Tiwari.*/`

## Python3

 `# Python 3 program to ` `# check the number is ` `# divisible by all  ` `# digits are not. ` ` `  `# Function to check  ` `# the divisibility  ` `# of the number by ` `# its digit. ` `def` `checkDivisibility(n, digit) : ` `     `  `    ``# If the digit divides the ` `    ``# number then return true ` `    ``# else return false. ` `    ``return` `(digit !``=` `0` `and` `n ``%` `digit ``=``=` `0``) ` `     `  `# Function to check if ` `# all digits of n divide ` `# it or not ` `def` `allDigitsDivide( n) : ` `     `  `    ``temp ``=` `n ` `    ``while` `(temp > ``0``) : ` `         `  `        ``# Taking the digit of ` `        ``# the number into digit ` `        ``# var. ` `        ``digit ``=` `n ``%` `10` `        ``if` `((checkDivisibility(n, digit)) ``=``=` `False``) : ` `            ``return` `False` ` `  `        ``temp ``=` `temp ``/``/` `10` `     `  `    ``return` `True` ` `  `# Driver function ` `n ``=` `128` ` `  `if` `(allDigitsDivide(n)) : ` `    ``print``(``"Yes"``) ` `else` `: ` `    ``print``(``"No"` `) ` `     `  `# This code is contributed by Nikita Tiwari. `

## C#

 `// C# program to check whether ` `// number is divisible by all its digits. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Function to check the divisibility ` `    ``// of the number by its digit. ` `    ``static` `bool` `checkDivisibility(``int` `n, ``int` `digit) ` `    ``{ ` `        ``// If the digit divides the number ` `        ``// then return true else return false. ` `        ``return` `(digit != 0 && n % digit == 0); ` `    ``} ` ` `  `    ``// Function to check if all ` `    ``// digits of n divide it or not, ` `    ``static` `bool` `allDigitsDivide(``int` `n) ` `    ``{ ` `        ``int` `temp = n; ` `        ``while` `(temp > 0) { ` ` `  `            ``// Taking the digit of the ` `            ``// number into var 'digit'. ` `            ``int` `digit = n % 10; ` ` `  `            ``if` `((checkDivisibility(n, digit)) == ``false``) ` `                ``return` `false``; ` ` `  `            ``temp /= 10; ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``// Driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 128; ` ` `  `        ``// funtion call to check ` `        ``// digits divisibility ` `        ``if` `(allDigitsDivide(n)) ` `            ``Console.WriteLine(``"Yes"``); ` ` `  `        ``else` `            ``Console.WriteLine(``"No"``); ` `    ``} ` `} ` ` `  `/*This code is contributed by vt_m.*/`

## PHP

 ` 0) { ` ` `  `        ``// Taking the digit of the ` `        ``// number into digit var. ` `        ``\$digit` `= ``\$n` `% 10; ` `         `  `        ``if` `(!(checkDivisibility(``\$n``, ``\$digit``))) ` `            ``return` `false; ` ` `  `        ``\$temp` `/= 10; ` `    ``} ` `     `  `    ``return` `true; ` `} ` ` `  `// Driver function ` ` `  `    ``\$n` `= 128; ` `    ``if` `(allDigitsDivide(``\$n``)) ` `        ``echo` `"Yes"``;  ` `    ``else` `        ``echo` `"No"``; ` `     `  `// This code is contributed by ajit. ` `?> `

Output:

```Yes
```

Alternate Implementation in Python

 `# Python 3 program to ` `# check the number is ` `# divisible by all  ` `# digits are not. ` `  `  `# Function to check  ` `# the divisibility  ` `# of the number by ` `# its digit. ` `def` `checkDivisibility(n, digit) : ` `      `  `    ``# If the digit divides the ` `    ``# number then return true ` `    ``# else return false. ` `    ``return` `(digit !``=` `0` `and` `n ``%` `digit ``=``=` `0``) ` `      `  `# Function to check if ` `# all digits of n divide ` `# it or not ` `def` `allDigitsDivide( n) : ` `    ``nlist ``=` `map``(``int``, ``set``(``str``(n))) ` `    ``for` `digit ``in` `nlist : ` `        ``if`  `not` `(checkDivisibility(n, digit)) : ` `            ``return` `False` `    ``return` `True` `      `  `  `  `# Driver function ` `n ``=` `128` `  `  `print``(``"Yes"` `if` `(allDigitsDivide(n)) ``else` `"No"``) `

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 : jit_t, sarathbapu

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.