Examples
- Hello World Program in Python (How to Print Hello World in Python?)
- Python Program to Add Two Numbers (Sum/Addition Program)
- How to Generate Random Number in Python? (Program)
- Leap Year Program in Python (Check Leap Year or Not in Python)
- Python Program to Check Number is Positive, Negative, or Zero
- Even Odd Program in Python (How to Check Number is Even or Odd?)
- Python Program to Find Largest of 3 Numbers (Greatest of Three Numbers in Python)
- Prime Number Program in Python (Check Prime or Not)
- Python Program to Find Square Root of a Number (Examples)
- Python Factorial Program (How to Find Factorial of a Number in Python?)
- Python Program to Find Prime Numbers in Range
- Python Program to Find Area of Triangle (Different Ways With Examples)
- How to Solve Quadratic Equation in Python? (Program Examples)
- How to Swap Two Numbers in Python? (Program Examples to Swap Variables)
- Python Program to Convert Kilometers to Miles (km to mi) and Vice Versa
- Python Program to Convert Celsius To Fahrenheit (and Fahrenheit to Celsius)
- Program to Check Armstrong Numbers in Python (Code Examples)
- Program for Armstrong Number Between Two Intervals in Python
- Program to Print Multiplication Table in Python (Code Examples)
- Python Program for Fibonacci Series (Print Fibonacci Sequence in Python)
- Program for Sum of n Natural Numbers in Python (Code Examples)
- Program to Print Powers of 2 in Python (Code Examples)
- Python Program to Find Numbers Divisible by Another Number
- Convert Decimal to Binary, Octal and Hexadecimal in Python (Program with Example)
- How to Convert Decimal to Binary in Python? Program with Examples
- How to Convert Decimal to Octal in Python? Program with Examples
- How to Convert Decimal to Hexadecimal in Python? Program and Examples
- How to Convert Binary to Decimal in Python? Program With Examples
- How to Convert Hexadecimal to Octal in Python? Program With Examples
- Program to Find LCM of Two Numbers in Python (Calculate LCM in Python)
- GCD of Two Numbers in Python (Program to Find HCF or GCD)
- GCD of Three Numbers in Python (HCF Program for n Numbers)
- Simple Calculator Program in Python (Basic Calculator Code)
- Find Factors of a Number in Python (Programs and Examples)
- Program to Find Prime Factors of a Number in Python
- How to Find ASCII Value in Python? ASCII Value of Character
Python Program to Find Numbers Divisible by Another Number
Let’s learn how to find numbers divisible by another number in Python! Here, we provide you with practical solutions to identify numbers that are evenly divisible by a given divisor using Python programming.
Divisibility is a fundamental concept in mathematics, and Python offers a versatile set of tools to tackle this problem efficiently. Whether you're a beginner looking to understand the basics or an experienced programmer seeking to optimize your code, this page will guide you through various Python programs for different needs and scenarios.
Our programs focus on simplicity and effectiveness, ensuring that you grasp the concepts quickly and can apply them to real-world situations. We will show how to use Python's syntax and powerful modulo operator to identify numbers that are divisible by a given divisor.
You will find concise code explanations and well-commented example code that showcases different approaches to solve this problem of printing the numbers divisible by another number in Python.
Find Numbers Divisible by Another Number in Python
Code
def find_divisible_numbers(numbers, divisor):
divisible_numbers = []
for number in numbers:
if number % divisor == 0:
divisible_numbers.append(number)
return divisible_numbers
# Example usage
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
divisor = 5
divisible_numbers = find_divisible_numbers(numbers, divisor)
print(f"Numbers divisible by {divisor}: {divisible_numbers}")
Output
Numbers divisible by 5: [5, 10]
Explanation
-
In this example, the find_divisible_numbers function takes a list of numbers and a divisor as input. It iterates over each number in the list and checks if it is divisible by the divisor using the modulo operator (%). If the number is divisible, it appends it to the divisible_numbers list.
You can customize the numbers list and divisor variable to fit your specific requirements. Running this program will output the numbers divisible by the given divisor.
Python Program to Check if a Number is Divisible by 3 and 5
Code
# Find numbers divisible by 3 and 5 in Python
def check_divisibility(number):
if number % 3 == 0 and number % 5 == 0:
return True
else:
return False
# Example usage
num = int(input("Enter a number: "))
if check_divisibility(num):
print(f"The number {num} is divisible by both 3 and 5.")
else:
print(f"The number {num} is not divisible by both 3 and 5.")
Output
Enter a number: 990
The number 990 is divisible by both 3 and 5
Explanation
-
In this program, we define a function called check_divisibility that takes a parameter number, which represents the number to be checked for divisibility.
-
Inside the function, we use the modulus operator % to check if the number is divisible by both 3 and 5. If the remainder of the division by both numbers is 0, it means the number is divisible by both. In that case, we return True. Otherwise, we return False.
-
In the example part, we prompt the user to enter a number using the input function and convert it to an integer using int(). We then call the check_divisibility function with the entered number as the argument.
Depending on the returned value, we display an appropriate message indicating whether the number is divisible by both 3 and 5 or not.
Python Program to Check if a Number is Divisible by 2
Code
# Check if number is divisible by 2 in Python
def check_divisibility(number):
if number % 2 == 0:
return True
else:
return False
# Example usage
num = int(input("Enter a number: "))
if check_divisibility(num):
print(f"The number {num} is divisible by 2.")
else:
print(f"The number {num} is not divisible by 2.")
Output
Enter a number: 77
The number 77 is not divisible by 2.
Explanation
-
In this program, we define a function called check_divisibility that takes a parameter number, representing the number to be checked for divisibility.
-
Inside the function, we use the modulus operator % to check if the number is divisible by 2. If the remainder of the division is 0, it means the number is divisible by 2. In that case, we return True. Otherwise, we return False.
-
In the example usage part, we prompt the user to enter a number using the input function and convert it to an integer using int(). We then call the check_divisibility function with the entered number as the argument.
Python Program to Check Whether a Number is Divisible by 5 and 11 or not
Code
# Check if a number is divisible by both 5 and 11
def check_divisibility(number):
if number % 5 == 0 and number % 11 == 0:
return True
else:
return False
# Example usage
num = int(input("Enter a number: "))
if check_divisibility(num):
print(f"The number {num} is divisible by both 5 and 11.")
else:
print(f"The number {num} is not divisible by both 5 and 11.")
Output
Enter a number: 55
The number 55 is divisible by both 5 and 11.
Explanation
-
We define a function called check_divisibility that takes a parameter number, representing the number to be checked for divisibility.
-
Inside the function, we use the modulus operator % to check if the number is divisible by both 5 and 11. If the remainder of the division by both numbers is 0, it means the number is divisible by both. In that case, we return True. Otherwise, we return False.
-
We prompt the user to enter a number using the input function and convert it to an integer using int(). We then call the check_divisibility function with the entered number as the argument.
Depending on the returned value, we display an appropriate message indicating whether the number is divisible by both 5 and 11 or not.
Practice More Python Programs:
- Hello World Program in Python
- Python Program to Add Two Numbers
- Generate Random Number in Python
- Leap Year Program in Python
- Even Odd Program in Python
- Python Program to Find Largest of 3 Numbers
- Prime Number Program in Python
- Python Program to Find Square Root of a Number
- Python Factorial Program
- Python Program to Find Area of Triangle
- Solve Quadratic Equation in Python
- Swap Two Numbers in Python
- Python Program to Convert Kilometers to Miles
- Python Program to Convert Celsius To Fahrenheit
- Check Armstrong Numbers in Python
- Print Multiplication Table in Python
- Python Program for Fibonacci Series
- Convert Decimal to Binary, Octal and Hexadecimal in Python
- Find LCM of Two Numbers in Python
- GCD of Two Numbers in Python
- Simple Calculator Program in Python
- Python Matrix Addition Program
- Transpose of Matrix in Python
- Python Matrix Multiplication
- Python Calendar Program