w3resource

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

Python Basic: Exercise-147 with Solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/python-basic-exercise-147.php