In this post, we will write a Python program to find numbers divisible by another number with detailed explanation and example.

Finding numbers divisible by another number is a simple Python program generally asked in school and college practicals. There are two different ways we can write this program.

- Using for-loop and conditional statements
- Using filter function

Let’s see both the ways along with its algorithms.

## Using For-loop and Conditional Statements

Our problem statement is as follows: Suppose you have a list of numbers, and you want to check if each number in the list is divisible by a given number or not.

We make our program dynamic by taking a list of numbers from the user, as well as the number we wish to check for divisibility.

Now let’s check the steps or algorithm for this program.

### Algorithms

- Ask the User to input the size of the list (
).`len_list`

- Take a list of numbers from the user (
).`num_list`

- Also take user input of number we wish to check for divisibility (
).`num`

- Create an empty list (
) to store the number which is divisible by`result`

.`num`

- Using for-loop iterate through the
.`num_list`

()`for i in num_list:`

- Inside the for-loop check
then append`if i % num == 0`

in the`i`

list.`result`

- print the
list.`result`

### Source Code

```
len_list = int(input("Enter size of the list: "))
num_list = [int(input(f"Enter {i+1} element: ")) for i in range(len_list)]
num = int(input("Enter a number we wish to check for divisibility: "))
result = []
for i in num_list:
if i % num == 0:
result.append(i)
print(f"Numbers divisible by {num} are: {result}")
```

### Output

```
Enter size of the list: 5
Enter 1 element: 7
Enter 2 element: 45
Enter 3 element: 14
Enter 4 element: 10
Enter 5 element: 49
Enter a number we wish to check for divisibility: 7
Numbers divisible by 7 are: [7, 14, 49]
```

## Using Filter Function

We will also write the above program using the filter function. Let’s first discuss the algorithm and then jump into the programming part.

### Algorithms

The first three steps are exactly the same as the algorithms mentioned above. After these three steps, follow the instructions below:

- Using filter function do this and store it in variable called
:`result`

`result = list(filter(lambda i: i % num == 0, num_list))`

- At the end print the
.`result`

### Source Code

```
len_list = int(input("Enter size of the list: "))
num_list = [int(input(f"Enter {i+1} element: ")) for i in range(len_list)]
num = int(input("Enter a number we wish to check for divisibility: "))
result = list(filter(lambda i: i % num == 0, num_list))
print(f"Numbers divisible by {num} are: {result}")
```

### Output

```
Enter size of the list: 5
Enter 1 element: 89
Enter 2 element: 26
Enter 3 element: 15
Enter 4 element: 13
Enter 5 element: 39
Enter a number we wish to check for divisibility: 13
Numbers divisible by 13 are: [26, 13, 39]
```

This is all about how we write a Python program to find numbers divisible by another number. I hope this post adds some value to your life. Thank you for reading, and I’ll see you in the next post.