Write a program to check the divisibility of a number by 7 that is passed as a parameter to the user defined function.
Program to check divisibility of a number by 7
Using user defined function with parameter
def checkDivisibility(num): if num % 7 == 0: print(num, "is divisible by 7") else: print(num, "is not divisible by 7")
Main program
num = int(input("Enter the number: ")) checkDivisibility(num)
Output: Enter the number: 49 49 is divisible by 7
Enter the number: 25 25 is not divisible by 7
Explanation
This question appears in the Suggested Lab Exercises section of the textbook. The solution follows the same pattern as Program 7-7 (calcFact example) where a user-defined function accepts a parameter. The function checkDivisibility() accepts 'num' as a parameter and uses the modulus operator (%) to check divisibility by 7. If the remainder is 0, the number is divisible by 7.
Solution Steps
Step 1: Define a user-defined function checkDivisibility() that accepts one parameter 'num'
Step 2: Inside the function, use modulus operator (%) to check if num % 7 equals 0
Step 3: If remainder is 0, print that number is divisible by 7, otherwise print not divisible
Step 4: In main program, accept a number from user using input() and convert to integer
Step 5: Call the function by passing the user-input number as argument