Question 8 of 17intermediate🔧 ApplyLong Answer4 marks

Write a program to check the divisibility of a number by 7 that is passed as a parameter to the user defined function.

Correct Answer

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

Exercise: SUGGESTED LAB. EXERCISES | Q: 1 | (Page 30)
For More Understanding

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

  1. Step 1: Define a user-defined function checkDivisibility() that accepts one parameter 'num'

  2. Step 2: Inside the function, use modulus operator (%) to check if num % 7 equals 0

  3. Step 3: If remainder is 0, print that number is divisible by 7, otherwise print not divisible

  4. Step 4: In main program, accept a number from user using input() and convert to integer

  5. Step 5: Call the function by passing the user-input number as argument