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!

Subscribe to my newsletter:

Donate!:

~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~

More content:
Facebook:
Google+:
Twitter:

Amazing Web Hosting – (The best web hosting for a cheap price!)

Nguồn: https://nonnaofitaly.com

Xem thêm bài viết khác: https://nonnaofitaly.com/cong-nghe/

21 comments

  1. Learn Javascript – http://bit.ly/JavaScriptPlaylist
    Learn Java – http://bit.ly/JavaPlaylist
    Learn C# – http://bit.ly/CSharpTutorialsPlaylist
    Learn C++ – http://bit.ly/CPlusPlusPlaylist
    Learn C – http://bit.ly/CTutorialsPlaylist

    Reply
  2. Thank you! I understand now what and how modulus operator is and works.

    Reply
  3. Sharing your pizza with your pet rat, imagine that 😀 great content, very clear videos.

    Reply
  4. Great educational video series, keep up the good work!

    Reply
  5. Am I the only one who got annoyed by the way he talks?

    Reply
  6. b) I'm a total geek and already know most of this stuff so I'm watching it at 1.5x speed

    Reply
  7. Good video

    Reply
  8. if i apply the modulus operator between 4 and 8 the remainder would be 0.5, but when I run the program the answer is 4, What is wrong with fractions?

    Reply
  9. me and my friend are both coders and would like for u to stop being so nerdy; we aren't all like this and it's disconcerting. that being said, good explanation… ur just corny as hell

    Reply
  10. Yo' IDE too dark can't see clearly

    Reply
  11. Can someone please explain what a modulator sign is? I'm so confused on how to work out a modulus operation

    Reply
  12. Straight to the point with great visual explanations and a fun attitude.
    Fantastic tutorial Caleb, keep it up!

    Reply
  13. https://youtu.be/zyK_cctGwYs

    Calculate modulus without using %
    Watch my video to click the above link…..thank you

    Reply
  14. wise words, about time, agreed.

    Reply
  15. what about the remainder of floating numbers

    Reply
  16. thanks

    Reply
  17. i gave myself this example: 13 % 3 is 1, because if all the 3 in 13 get eaten there will be 1 left
    there is four "3" inside 13
    1 is extra and it remains
    or 1 doesn't count as 3 so it remains

    Reply
  18. i love your vids thank you

    Reply
  19. i guess this is what haha sounds like when someone texts it

    Reply
  20. you are really annoying

    Reply
  21. I want the modulus where the first number is smaller than the second number

    Reply

Leave a Reply