Python Program to Find Numbers Divisible by Another Number
Last Updated :
05 Sep, 2024
We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number.
Examples:
Input: list=[8, 14, 21, 36, 43], num=3
Output: 21, 36, 57
Input: list=[2, 17, 25, 31, 48, 55], num=5
Output: 25, 55
In this article, we will discuss the different ways to find numbers divisible by another number in Python.
Finding Numbers Divisible by Another Number
To check if a number is completely divisible by another number, we use Python modulo operator, which is a part of the arithmetic operators. If the number is completely divisible by another number, it should return 0 as the remainder. If the remainder is other than 0, that means that the number is not completely divisible.
Now, let us see a few different ways to find all the numbers in a given list that are divisible by another number.
For Loop
In this example, we will use Python for loop to iterate over each element in the list. Then the extracted element id is checked for divisibility with the divisor by using the modulo operator. If it returns 0, then that number is added to another list that contains all the elements that were divisible by the divisor.
Python
def find_divisibles(myList, num):
# list to store divisible numbers
divisibles = []
# for loop for iteration
for i in myList:
# checking if the number is divisible
if i % num == 0:
# adding number to new list
divisibles.append(i)
return divisibles
myList = [8, 14, 21, 36, 43, 57, 63, 71, 83, 93]
num = 3
print(find_divisibles(myList, num))
Output:
[21, 36, 57, 63, 93]
List Comprehension
List comprehensions provide a concise way to generate lists. We can use this feature to create a list of numbers that are divisible by another number in a single line of code.
Python
def find_divisibles(myList, num):
# list comprehension to check if the number is divisible
return [i for i in myList if i % num == 0]
myList = [8, 14, 21, 36, 43, 57, 63, 71, 83, 93]
num = 3
print(find_divisibles(myList, num))
Output:
[21, 36, 57, 63, 93]
The filter() and Lambda Functions
The filter() function in Python can be used to construct an iterable from elements of another iterable that satisfy a specific condition. When combined with a lambda expression, it provides a clean way to filter out numbers based on divisibility. The list() function is used to convert the result back into a list.
Python
def find_divisibles(myList, num):
# filter() and lambda functions
return list(filter(lambda i: i % num == 0, myList))
myList = [8, 14, 21, 36, 43, 57, 63, 71, 83, 93]
num = 3
print(find_divisibles(myList, num))
Output:
[21, 36, 57, 63, 93]
Similar Reads
Python Program to print all the numbers divisible by 3 and 5 for a given number Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are
2 min read
Program to print all the numbers divisible by 3 and 5 for a given number Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are
9 min read
Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range Given a range of numbers, the task is to write a Python program to find numbers divisible by 7 and multiple of 5. Example: Input:Enter minimum 100 Enter maximum 200 Output: 105 is divisible by 7 and 5. 140 is divisible by 7 and 5. 175 is divisible by 7 and 5. Input:Input:Enter minimum 29 Enter maxim
5 min read
Python Program to Find XOR of array elements which are divisible by given number Given an array arr[] containing integers of size N and a number k, the task is to find the XOR of array elements that are divisible by a given number k in Python.Examples: Input: arr[] = {3, 9, 16, 12, 13, 15} , k = 3Output: 25Explanation: Only 3,9,12,15 are divisible by 3, XOR = 3^9^12^15 = 9Input:
4 min read
Python Program to Check if a Number is Odd or Even Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1.In this article, we will learn how to check if
2 min read