## C Programming Tutorial 32 – Modulus Operator

This video is going to introduce you to a new operator called the modulus operator (%).

In the last video I gave you this example:

int y = 5 / 2;

I told you that the value of y is going to be 1. Did you figure out what this operator does? Well it actually takes the remainder. I’ve always been pretty bad at visualizing a remainder, so I use imagery. Imagine you have a pizza with 5 slices and you want to share the pizza with your pet rat, Pepper Jack.

Well you give a piece to Pepper Jack, a piece to you, a piece to pepper jack, and then another piece to you. Now, you have one left over:

int piecesOfPizza = 5;
int numberOfEaters = 2;
int leftOver = piecesOfPizza % numberOfEaters;

Now you might suggest just ripping the pizza in half, but you have to realize that piecesOfPizza is an integer. This means that we cannot split the piece in half.

This brings up the good point that the modulus operator is designed to work with integers. That’s because if you were using numbers of type double here, you and your pet rat would both get 2.5 pieces of pizza and the remainder would be zero. The modulus operator would be useless working with floating point numbers.

You can do a lot of cool things with the modulus operator. One example is figuring out if a number is even or odd. if you divide a number by 2 and the remainder is anything but 0, then you do not have an odd number.

That is all for the modulus operator. In the next video we are going to learn our first unary operator.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Support me! http://www.patreon.com/calebcurry

Subscribe to my newsletter: http://eepurl.com/-8qtH

Donate!: http://bit.ly/DonateCTVM2.

More content: http://CalebCurry.com

Amazing Web Hosting – https://www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

• Views:7,353 views
• Rating:
•  Click on the stars above to rate this page
• Categories:
• Tags:

The Epic Gamer Pro says:

thank you !

Mc Mihion says:

After the words of wisdom…I’m 39, starting c for scratch. It’s never too late.

A. I am totally enjoying your c videos

what software do you use?

Brett Sullivan says:

Can you teach c+ and c++?

X says:

This was so easy to understand and you were a fun teacher. Thanks for this vid!

MisterBassBoost says:

I enjoyed your video 🙂 Keep it up!

mark muscle says:

why is 9%10 = 9?
i can’t wrap my head around this..lol
btw nice video :)!

UnboxModz says:

well you do good videos they help alot
and i want to be a reverse engineer so yeah im still there learning C lol
but with your videos ill learn Fast

Mike Ellis says:

Your a funny dude 😉