Lesson 02-11: Checking for Divisibility

Learning Target: I can create a conditional to check for divisibility.

One of the first skills a programmer learns using conditionals is the skill of testing for divisibility. The first most basic example is to check whether a number is odd or even.

Odd / Even

Recall that a number is even if it is divisible by two, and a number is odd if it is not divisible by two.

To check whether a number is divisible by two, let’s first explore our own thought processes. Let’s take a few examples and examine them.

  • \(5\div2 = 2.5 \Longrightarrow\) is 5 divisible by 2? How do you know?
  • \(37\div2 = 18.5 \Longrightarrow\) is 5 divisible by 2? How do you know?
  • \(14\div2 = 7.5 \Longrightarrow\) is 5 divisible by 2? How do you know?
  • \(100\div2 = 50 \Longrightarrow\) is 5 divisible by 2? How do you know?
  • \(26\div2 = 13 \Longrightarrow\) is 5 divisible by 2? How do you know?
  • \(44\div2 = 22 \Longrightarrow\) is 5 divisible by 2? How do you know?

Notice the pattern? Whenever a number is not divisible by 2, it has a decimal - or in other words, it has a remainder. We have something that can calculate remainder - what was it?

    ch0211-1: What can we use to calculate remainder after division?
  • (A) **
  • That represents an exponent!
  • (B) float()
  • That's a conversion function!
  • (C) %
  • That's Modulo!
  • (D) remainder()
  • That doesn't exist!

Great! Except this time, we want to check if an expression has NO remainder. Let’s start by writing what we know.

In order to check whether an integer number is divisible by 2, the expression number % 2 will give us the remainder after division. If number is even, then it means 2 goes into it without remainder. This means that the remainder should be zero. So we want to check if number % 2 will be zero.

How do you represent number % 2 equals zero as a boolean expression? don't forget how to represent 'equals' in python! Try again! You should be writing a valid boolean expression.

If you’re at this point, great job! We have a boolean expression to represent whether a number is divisible by a 2 or not! Feel free to try it out in the space below:

Checking For Divisibility

Let’s try to generalize this rule so it will work with any number:

Right now, number % 2 == 0 checks for divisibility by 2.

  • If we wanted to check for divisibility by 3, we would use the expression number % 3 == 0
  • If we wanted to check for divisibility by 4, we would use the expression number % 4 == 0
  • If we wanted to check for divisibility by 5, we would use the expression number % 5 == 0

Notice the pattern?

So we can generalize by saying: If we want to check for divisibility by N, we can use the expression number % N == 0 that will be True if it is divisible and False otherwise.

Checks for Understanding

Q#1

Write a boolean expression that represents divisibility by 10. don't forget how to represent 'equals' in python! Try again! You should be writing a valid boolean expression.

Q#2

Write a boolean expression that represents divisibility by 7. don't forget how to represent 'equals' in python! Try again! You should be writing a valid boolean expression.

Q#3

Write a boolean expression that represents divisibility by 12. don't forget how to represent 'equals' in python! Try again! You should be writing a valid boolean expression.

Next Section - Unit 2 Pre-Lab Check