Python Program for Check if all Digits of a Number Divide It

Last Updated : 31 Oct, 2025

Given a number N, the task is to check whether all digits of the number divide it completely or not. In simple terms, every non-zero digit in the number should divide N evenly. For example:

Input: 128
Output: Yes -> (128 % 1 == 0, 128 % 2 == 0, 128 % 8 == 0)

Input: 130
Output: No -> (130 % 3 != 0)

Let’s explore different ways to solve this efficiently in Python.

Using all() with String Iteration

It converts the number into a string, checks each digit (ignoring zeros), and uses all() to verify that every digit divides the number evenly.

python
n = 128
if all(int(d) != 0 and n % int(d) == 0 for d in str(n)):
    print("Yes")
else:
    print("No")

Output
Yes

Explanation:

  • str(n) converts the number into a string for easy digit iteration.
  • int(d) != 0 ensures we don’t divide by zero.
  • n % int(d) == 0 checks divisibility.
  • all() returns True only if all conditions are True for every digit.

Using Mathematical Operations

This method extracts each digit mathematically using modulus and division, checking divisibility as it goes.

Python
n = 128
temp = n
flag = True

while temp > 0:
    digit = temp % 10
    if digit == 0 or n % digit != 0:
        flag = False
        break
    temp //= 10

if flag:
    print("Yes")
else:
    print("No")

Output
Yes

Explanation:

  • temp % 10 extracts the last digit of the number.
  • Checks if the digit divides n without remainder.
  • If any digit fails, loop stops early using break.
  • temp //= 10 removes the last digit for the next iteration.

Using map() and list() Conversion

This method converts digits to a list of integers, then iterates through each digit to check divisibility.

Python
n = 128
digits = list(map(int, str(n)))
count = 0

for d in digits:
    if d != 0 and n % d == 0:
        count += 1

if count == len(digits):
    print("Yes")
else:
    print("No")

Output
Yes

Explanation:

  • map(int, str(n)) converts each character to integer digits.
  • Each digit is checked if non-zero and divides n.
  • The count of valid digits is compared with total digits.

Using List Comprehension and sum()

This approach checks divisibility using list comprehension and compares how many digits divide the number.

Python
n = 128
digits = [int(d) for d in str(n)]
res = [d for d in digits if d != 0 and n % d == 0]

if len(res) == len(digits):
    print("Yes")
else:
    print("No")

Output
Yes

Explanation:

  • The first list extracts all digits from the number.
  • The second list keeps only those digits that divide the number evenly.
  • Compares both list lengths if all divide, result is “Yes”.

Please refer complete article on Check if all digits of a number divide it for more details!

Comment