C Programming Tutorial 32 - Modulus Operator

submitted by mkenny400 on 03/19/18 1

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! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×