Lesson goal: Divisibility of numbers

Previous: Dividing by zero | Home | Next: Literal vs. to-be text

You might remember in your math class a lesson on "divisibility" of numbers. That is, does a given number have other numbers that divide evenly into it. These are sometimes called the "factors" of a number. For example, the number 35 is divisible by 1, 5 and 7. Stated another way, 1, 5, and 7 are "factors" of 35.

As you move along with this concept, there is a list of "rules" you can use to see if a number might have factors. For example, even numbers are all divisible by 2. For 3, you'd add up the digits in a number. If the sum is divisible by 3, then so is the whole number (strange, huh?).

We say "forget the divisility" rules. Testing for divisbility is incredibly easy on the computer using the % or "mod" operator. If you recall from this lesson, the % operator divides two numbers and tells us the remainder (not the quotient). So if use the mod operator on two numbers, say as in A % B, and if we get a zero remainder, then A is divisible by B, or B must be a factor of A!

A%B
Move the mouse over a dotted box for more information.

Now you try. Use the print statement to test the divisibility of some numbers. Is 492 divisible by 3? Is 2092 divisible by 4? Use the % operator. If you get zero for a remainder, then the answer to "is it divisible by.." is "yes!"

Type your code here:


See your results here:

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.