w3resource

Python: Function to check whether a number is divisible by another number


Divisibility Tester

Write a Python function to check whether a number is divisible by another number. Accept two integer values from the user.

Sample Solution:

Python Code :

# Define a function named 'multiple' that takes two arguments, 'm' and 'n'.
def multiple(m, n):
    # Check if 'm' is a multiple of 'n' by using the modulo operator.
    # If the remainder is 0, return True, indicating 'm' is a multiple of 'n'.
    # Otherwise, return False.
    return True if m % n == 0 else False

# Call the 'multiple' function with different arguments and print the results.
print(multiple(20, 5))  # Check if 20 is a multiple of 5 and print the result.
print(multiple(7, 2))   # Check if 7 is a multiple of 2 and print the result.

Sample Output:

True
False

Pictorial Presentation:

Python: Function to check whether a number is divisible by another number.

Flowchart:

Flowchart: Function to check whether a number is divisible by another number.

Python Code Editor :

 

Previous: Write a Python program to find the location of Python module sources.
Next: Write a Python function to find the maximum and minimum numbers from a sequence of numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.