Advertisement
Felanpro

The Beauty of Modulus

Dec 28th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. /// The beauty of the Modulus ///
  2.  
  3.     /* Modulus, wow, almost forgot about this one.
  4.     If you don't know, Modulus, also called
  5.     Modulo Operation, or just Modulo; is one of the basic
  6.     arithmetic operators. The modulo operator returns
  7.     the remainder after division between two numbers.
  8.     Now you might think this is no big deal, at least in real
  9.     life on a paper, but in programming it plays an essential role in
  10.     all kinds of source codes. With the help of modulo, you can find
  11.     out if numbers are odd or even. Look at the following example
  12.     below:
  13.     */
  14.  
  15.     int x = 14;
  16.  
  17.     /* As you probably know, the number 14 is even
  18.     because it's divisible by 2 and does not return a
  19.     value with decimals.
  20.     This is the point to where new programmers often encounter
  21.     some basic problems. "How do I check if a number is even in
  22.     programming?", might occur while sitting in front of the
  23.     monitor. They often divide the number by 2, but then they're
  24.     just lost. Is there a function which will tell me all this?,
  25.     do I need to use a for loop or what do I do? The answer is Modulo.
  26.     By simply using it you can easily check if a number is even or odd.
  27.     */
  28.  
  29.     cout << x % 2;
  30.     /*
  31.     OUTPUT:
  32.     0
  33.     */
  34.  
  35.     /* By using the modulo operator we received the number 0, which means that
  36.     there was no remainder, which in turn means that the number 14 is even.
  37.     Let's look at an opposite example.
  38.     */
  39.  
  40.     int y = 7;
  41.  
  42.     /*
  43.     We know that this number is odd.
  44.     */
  45.  
  46.     cout << y % 2;
  47.     /*
  48.     OUTPUT:
  49.     1
  50.     */
  51.  
  52.     /*
  53.     This gave us the remainder of 1, which means that the number 7 is odd.
  54.     Sorry for the long text, i'm writing this late and have no idea what to
  55.     do else, so i'm sharing my knowledge of something instead.
  56.     */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement