# Check whether all digits of a number divide it using Python

Today, in this tutorial, we will get to know how to **check whether all the digits of a number divide it using a Python program**. We will check the divisibility of the given number by each digit of that number. So, you will learn how to get individual digits of a number, a method to check whether the number is divisible by its digits, and a Python program for the same. If you want the Python program source code, you are in the right place.

## Method to get the digits of a number

To get the individual digits of a number, follow the steps given below-

- Declare an array to store the digits of a number.
- Check whether the number is 0.
- Go to the next step if the condition is false. Otherwise, go to the last step.
- Get the remainder when the number is divided by 10.
- Store this remainder in the array.
- Divide the number by 10.
- The array stores the individual digits of the given number.

To understand the above steps, let us take an example.

Suppose the number is 5354, so we will find the individual digits by following the above steps.

Let number = 5354 Initially array_digits = [] Iteration 1 5354 > 0 (True) array_digits[0] = 5354 % 10 = 4 number = 5354 // 10 = 535 Iteration 2 535 > 0 (True) array_digits[1] = 535 % 10 = 5 number = 535 // 10 = 53 Iteration 3 53 > 0 (True) array_digits[2] = 53 % 10 = 3 number = 53 // 10 = 5 Iteration 4 5 > 0 (True) array_digits[3] = 5 % 10 = 5 number = 5 // 10 = 0 Iteration 5 0 > 0 (False) array_digits = [4, 5, 3, 5]

## Python program to check whether all digits of a number divide it

Now, we will see a Python program that checks whether all the digits of a number divides it. Firstly, we will take the number from the user as an input. Then, using the above method, we will find the individual digits of that number. After finding an individual digit, we have to check the divisibility of the number by this digit. If the number is not divisible, we conclude that the number is not divisible by all the digits of that number. And if the number is divisible, we continue this until the divisibility of the number is checked by all the individual digits. The Python program is given below-

def check_divisible(number): temp = number while(temp > 0): digit = number % 10 if (digit != 0 and number % digit==0): return False temp = temp // 10 return True num = int(input("ENTER A NUMBER : ")) if(check_divisible(num)): print("ALL THE DIGITS OF NUMBER",num,"DIVIDE IT") else : print("ALL THE DIGITS OF NUMBER",num,"DONOT DIVIDE IT")

In the above program, the function *‘check_divisible’* checks whether all the digits of a number divide it. It returns false if the number is not divisible by any of its digit. Otherwise, it returns true. Finally, we display the result based on value (true/false) returned by the function.

#### Python program output

The above Python program tells whether all the individual digits of a given number divide it or not. The output of the program after sample execution is as follows-

**Checking for number 450**

[email protected]:~/python$ python3 digits.py ENTER A NUMBER : 450 ALL THE DIGITS OF NUMBER 450 DIVIDE IT [email protected]:~/python$

So, the number 450 is divisible by all its digits i.e. 4, 5, and 0.

**Checking for number 211**

[email protected]:~/python$ python3 digits.py ENTER A NUMBER : 211 ALL THE DIGITS OF NUMBER 211 DONOT DIVIDE IT [email protected]:~/python$

So, the number 211 is not divisible by all its digits i.e. 2, 1, and 1. Because the number 211 is not divisible by 2.

Also read: Convert doc file to PDF in Python

## Leave a Reply