Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// The beauty of the Modulus ///
- /* Modulus, wow, almost forgot about this one.
- If you don't know, Modulus, also called
- Modulo Operation, or just Modulo; is one of the basic
- arithmetic operators. The modulo operator returns
- the remainder after division between two numbers.
- Now you might think this is no big deal, at least in real
- life on a paper, but in programming it plays an essential role in
- all kinds of source codes. With the help of modulo, you can find
- out if numbers are odd or even. Look at the following example
- below:
- */
- int x = 14;
- /* As you probably know, the number 14 is even
- because it's divisible by 2 and does not return a
- value with decimals.
- This is the point to where new programmers often encounter
- some basic problems. "How do I check if a number is even in
- programming?", might occur while sitting in front of the
- monitor. They often divide the number by 2, but then they're
- just lost. Is there a function which will tell me all this?,
- do I need to use a for loop or what do I do? The answer is Modulo.
- By simply using it you can easily check if a number is even or odd.
- */
- cout << x % 2;
- /*
- OUTPUT:
- 0
- */
- /* By using the modulo operator we received the number 0, which means that
- there was no remainder, which in turn means that the number 14 is even.
- Let's look at an opposite example.
- */
- int y = 7;
- /*
- We know that this number is odd.
- */
- cout << y % 2;
- /*
- OUTPUT:
- 1
- */
- /*
- This gave us the remainder of 1, which means that the number 7 is odd.
- Sorry for the long text, i'm writing this late and have no idea what to
- do else, so i'm sharing my knowledge of something instead.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement