Skip to content
Suggested Lab. Exercises · Q1

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

Punjab PsebTextbookSubjective· 3mImportance★★★★★est
68% · 27/40 Questions
✓ Free question

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

  • n is read and converted with int(); the call divisible_by_7(n) binds it to the parameter num.
  • 49 % 7 is 0 → the if branch prints the "divisible" message; 30 % 7 is 2 → the else branch runs.
  • Negative multiples work too: -14 % 7 is 0 in Python, so −14 is correctly reported divisible.
Tip

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.

✓Final answer

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.