Q.Write a program to check the divisibility of a number by 7 that is passed as a parameter to the user defined function.
Pass the number as a parameter to a user-defined function and test num % 7 == 0 — remainder zero means divisible; e.g. 49 is divisible by 7, 30 is not.
The idea. Divisibility is a remainder question, so the modulus operator is the whole algorithm: num % 7 is 0 exactly when 7 divides num. The exercise's real point is the function mechanics — the value is read in the main program and handed to the function as a parameter, as the question requires.
def divisible_by_7(num):
# Report whether num is divisible by 7
if num % 7 == 0:
print(num, "is divisible by 7")
else:
print(num, "is not divisible by 7")
n = int(input("Enter a number: "))
divisible_by_7(n) # the number is passed as an argument
Sample runs
Enter a number: 49
49 is divisible by 7
Enter a number: 30
30 is not divisible by 7
How it works
nis read and converted withint(); the calldivisible_by_7(n)binds it to the parameternum.49 % 7is 0 → theifbranch prints the "divisible" message;30 % 7is 2 → theelsebranch runs.- Negative multiples work too:
-14 % 7is 0 in Python, so −14 is correctly reported divisible.
A variant that returns a Boolean (return num % 7 == 0) is even more reusable — the caller can then count multiples of 7 in a range, filter a list, etc., instead of only printing.
The function reports divisibility via the remainder test num % 7 == 0: input 49 → "49 is divisible by 7"; input 30 → "30 is not divisible by 7".
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.