# The Remainder Operator

## It might be Mathematical, but you need this in order to be an effective programmer be it Java, Python or Swift. Trust me…

It is common for tutorials to assume that you know the remainder operator. But what is it, and why do we need it?

Difficulty: **Beginner** | Easy** **| Normal | Challenging

# Terminology

Dividend: The amount you want to divide up

Divisor: Is another number by which another number is divided

Integer: A number that has no fractional part, that is no digits after the decimal point

Remainder: The remainder that is left over when a number is divided by another

Modulus: Another term for remainder

Division (Integer): A division in which the fractional part (remainder) is discarded

# Integer division

Integer division involves *whole numbers*. This means that we are only interested in whole numbers.

Let us think this through.

We are dividing a whole number by a whole number, and are only interested in a whole number as a result.

Let us take some examples:

so

`6 / 2 = 3`

`10 / 2 = 5`

Which, I’d venture is the results you’d expect.

But what about dividing one by two?

`4 / 3 = 1`

What just happened? Remember, we are only thinking about *whole numbers here*.

So there is a single 3, or to state it again `4 / 3 = 1`

Let us take another example.

`6 / 3`

You can “fit” two three’s into a six which means that there is no “gap”.

That is:

`6 / 3 = 2`

So effectively we are counting the complete number of divisors we can fit into the dividend.

We are only dealing with **whole numbers.** That is, this is Integer division.

# The remainder and modulo operation

The remainder is the value left over after we have performed the Integer division above.

Looking at `4 % 3`

and the accompanying diagram:

We are counting the “gap” (the red block) between a whole number of 3’s that can fit into 4, and the 4.

The language of the calculation is the same as before (although we have the % operator).

To put it another way, we take the dividend and divide it by the divisor, and this leaves up the remainder.

Let us take another example.

`6 / 3`

You can “fit” two three’s into a six which means that there is no remainder at all!.

That is:

`6 / 3 = 2`

and

`6 % 3 = 0`

# Odd and even numbers

In whichever language you choose, you might be asked some classic interview questions (hello fizzbuzz) based around the modulo operation.

Basically, we need to know if a number is even.

Oh, and **zero is counted as even.**

so we test where the divisor is 2

`0 % 2 = 0`

`1 % 2 = 1`

`2 % 2 = 0`

`3 % 2 = 1`

`4 % 2 = 0`

Where the results that equal zero are even, those that equal one are odd.

# Conclusion…

We are often expected to know how remainder works in any particular programming language. This can be tricky if school is fading into the background for you, or perhaps you were not adequately taught in the first place.

This tutorial has run through

- Remainder
- Modulus
- Odd and even numbers

If you’ve any comments or suggestions, please do get in touch!

# The repo link:

I’ve put the code for Swift, Java and Python in this repo.

# The Twitter contact:

Any questions? You can get in touch with me here