# Python Program for Check if all digits of a number divide it

358 views

In this program, you’ll learn to how to check for given a number n, find whether all digits of n divide it or not.

Suppose you have given a number 128, then we divide the number by digits at different places like one’s place, ten’s place, hundred’s place. i.e.

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

Input : 130 Output : No ```

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.

### Python Source Code to Check if all digits of a number divide it

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```# 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 = temp % 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" )

```Code language: Python (python)```

OUTPUT

`Yes`

Hope this was useful to you , check out other example, Keep Learning!

0 comment