• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Caleb Curry

Making Money and Investing

  • Home
  • Make Money Online
  • How to Buy Bitcoin
  • Get Started with Real Estate Investing
  • How to Start a Website
  • Courses
    • Full Time Income Online
  • Sponsorship

C Programming Tutorial 32 – Modulus Operator

October 18, 2018 by Caleb Curry Leave a Comment

Did you read the last blog on Arithmetic Operators? Are you new here? Start at the beginning, Intro to C!

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

In the last video I gave you this example:

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:

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.

In the next blog we are going to be discussing all Unary Plus and Minus. Check it out!

Filed Under: Uncategorized Tagged With: c programming, modulus operator

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

my face to show my face

My name is Caleb Curry and I started my online business teaching software development and doing digital marketing for brands. Entrepreneurship has changed my life and has allowed me to earn a full time living from anywhere. I now have the freedom and energy to pursue skills and opportunities that I find interesting. This website is all about my journey.

Get my top Python Tips and Tricks

50+ Python Tips and Tricks!

Subscribe to our newsletter to get notes and latest content by email.

    We won't send you spam. Unsubscribe at any time.

    Follow Me!

    • Instagram
    • YouTube

    Recent Posts

    Copyright © 2021 · Genesis Sample on Genesis Framework · WordPress · Log in