2.5: Numerical Operators

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    2.5: Numerical Operators

    Following table provides a list of commonly used numerical operators used in C++ programs. We have used the multiplication operator ' * ' in the previous sample program. We may need to use the rest of them in the future assignments. Please take a moment to get familiar with them, especially the integer division operator ' / ' and the modulus division operator ' % '. Please study the notes that follow the table.

    Operator Name Example Result
    + Addition 34+2 36
    - Subtraction 12-7 5
    * Multiplication 15*3 75
    / Division 12/4 3
    % Modulus 20%3 2

    Note 1:

    By the time we reach adulthood, we normally think of division as resulting in an answer that might have a fractional part (a floating-point data type). This type of division is known as floating-point division. However, division, when both operands are of the integer data type, may act differently, and is called: integer division. Consider:

    11 / 2

    We may think the result is 5.5. But because both operands are of the integer data type the evaluation of the expression (or answer) would be 2 with no fractional part (it gets thrown away). Again, this type of division is called integer division.

    Thus, following assignment statement will assign integer 2 to the variable a instead of 2.5

    a = 5/2;

    If we do want to send 2.5 to the variable a, we will need to write the code as following:

    a = 5/2.0;

    Pay special attention to following example:

    a = 7/10;

    This statement will send 0 to the variable a. 7 divided by 10 is 0.7, but since both numerator and the denominator are integers, the operator ' / ' will only result in 0 instead of 0.7. Again, if we do want to keep the digit after the decimal point, we will write the statement as

    a = 7.0/10;

    Note 2:

    ' % ' is a modulus division operator. This operator produces a remainder instead of quotient. Consider following example:

    a=11%4;

    We know when we divide 4 into 11

    Image 2-4-1.png

    the remainder is 3, thus the statement assign 3 to the variable a.

    We don't have this operator in algebra. So, please take time to get familiar with this special operator. Here, I present a few examples to help you understand and get familiar with this operator:

    • if a = 20%3, then a is 2 (3 goes into 10 six times and the remainder is 2)
    • if a = 3%7, then a is 3 (7 goes into 3 zero times and the remainder is 3)
    • if a = 4%10, then a is 4 (10 goes into 4 zero times and the remainder is 4)
    • if 2837%10, then a is 7 (10 goes into 2837 two hundred eighty-three times and the remainder is 7)

    In C++ programming, some of the numerical calculation will be done by using build-in functions. For example, we don't have a key such as xy on the computer keyboard, thus we use following function to perform exponential calculation:

    pow()

    For example, if want to calculate 24 and assign the result to a variable m, we will write following code:

    int m;
    m=pow(2,4)

    pow(a, b) is an exponential operation function (we will discuss more about C++ functions in the future). This function takes two inputs a and b, and calculates the value of an algebraic expression ab . For example, pow(2.0, 3) will produce the third power of 2. pow(3, 5) will produce the fifth power of 3 which is 243.


    This page titled 2.5: Numerical Operators was last modified on Wed, 24 Dec 2025 20:37:18 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.