Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Multiple of 7 or Ends with 7

Combine two number rules: divisibility by seven or a last digit of seven. A Python beginner lesson on named boolean values and inclusive or conditions.

IntermediatePhase 1 - Level 3: Math and Number LogicExample 5 of 10
multiple-of-7-or-ends-with-7.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Check conditions
5is_multiple = num % 7 == 0
6ends_with_7 = num % 10 == 7
7
8if is_multiple or ends_with_7:
9 print("Number is multiple of 7 or ends with 7")
10else:
11 print("Number is neither multiple of 7 nor ends with 7")

Output

Enter a number: 14
Number is multiple of 7 or ends with 7

Enter a number: 17
Number is multiple of 7 or ends with 7

Enter a number: 15
Number is neither multiple of 7 nor ends with 7

In this one

  • Store named booleans
  • Combine alternatives with or
  • Understand negative remainders

How it works

  1. 1

    Read num and calculate both remainder tests.

  2. 2

    Store is_multiple and ends_with_7.

  3. 3

    Print success when either boolean is true.

Edge cases

Does 77 pass?

Does 77 pass?

Yes. Both conditions are true, and or still accepts it.

Does -17 satisfy the ending test?

Does -17 satisfy the ending test?

No. In Python -17 % 10 is 3; this is not a sign-insensitive text check.

What's going on

The program stores two yes-or-no answers before combining them. is_multiple checks remainder seven; ends_with_7 checks remainder ten for the nonnegative decimal case.

or accepts either answer, including when both are true. Thus 14 and 17 both pass for different reasons. Python remainder on negatives follows the divisor's sign, so a negative number's written last digit needs abs before this test.

Does 77 pass? Yes. Both conditions are true, and or still accepts it.

Does -17 satisfy the ending test? No. In Python -17 % 10 is 3; this is not a sign-insensitive text check.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which operator accepts either rule?

Pick an answer to check it.

Question 2

Does 77 pass?

Pick an answer to check it.

Questions people ask

Does 77 pass?

Yes. Both conditions are true, and or still accepts it.

Does -17 satisfy the ending test?

No. In Python -17 % 10 is 3; this is not a sign-insensitive text check.

Which operator accepts either rule?

or.