Core C++

Lesson 21 : Arithmetical Operators


In this C++ tutorial, we cover the five arithmetical operators: Addition, Subtraction, Multiplication, Division, and Modulus. These operations should be familiar, except perhaps the modulus operator. For positive numbers, the modulus operator can be thought of as the remainder that we get from long division.

If it has been a while since you did long division, take a look at the image above. Here, we divide 100 by 27. In this example, 100 is called the dividend, 27 is called the divisor, 3 is the quotient and 19 is the remainder. So, we have 100 = 3*27 + 19. Using the C++ syntax, we get 100 / 27 = 3 and 100 % 27 = 19. Of course, all of this only applies to integer arguments.

All five of these operators are binary, which means that they take two arguments. However, should note that the modulus operator does not exist in C++ for doubles, since doubles can represent fractions. For example, if we calculate 100 / 27, we get 3.703703704 instead of the 3 that we get for integers.

 

© 2007–2024 XoaX.net LLC. All rights reserved.