Skip to main content
Engineering LibreTexts

4.9: Arithmetric Operators

  • Page ID
    29041
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Basic Operators

    An operator performs an action on one or more operands. The common arithmetic operators are:

    Action C++ Operator Symbol
    Addition +
    Subtraction -
    Multiplication *
    Division /
    Modulus (associated with integers) %

    These arithmetic operators are binary that is they have two operands. The operands may be either constants or variables.

    age + 1

    This expression consists of one operator (addition) which has two operands. The first is represented by a variable named age and the second is a literal constant. If age had a value of 14 then the expression would evaluate (or be equal to) 15.

    These operators work as you have learned them throughout your life with the exception of division and modulus. We normally think of division as resulting in an answer that might have a fractional part (a floating-point data type). However, division when both operands are of the integer data type act differently. Please refer to the supplemental materials on "Integer Division and Modulus".

    #include <iostream> 
    using namespace std; 
    
    int main() 
    { 
        int myNum1 = 10, myNum2 = 4, result; 
    
        // printing a and myNum2 
        cout<<"myNum1 is "<< myNum1 <<" and b is "<<b<<"\n"; 
    
       // addition
       result = myNum1 + myNum2;
       cout << "myNum1 + myNum2 is: "<< result << "\n";
    
       // subtraction
       result = myNum1 - myNum2;
       cout << "myNum1 - myNum2 is: "<< result << "\n";
    
       // multiplication
       result = myNum1 * myNum2;
       cout << "myNum1 * myNum2 is: "<< result << "\n";
    
       // division
       result = myNum1 / myNum2;
       // since 
       cout << "myNum1 / myNum2 is: "<< result << "\n";
    
       // modulus
       result = myNum1 % myNum2;
       cout << "myNum1 % myNum2 is: "<< result << "\n";
    
       return 0;
    }
    

    Output:

    myNum1 is 10 and myNum2 is: 4
    myNum1 + myNum2 is: 14
    myNum1 - myNum2 is: 6
    myNum1 * myNum2 is: 40
    myNum1 / myNum2 is: 2
    myNum1 % myNum2 is: 2
    

    Adapted from:
    " Arithmetic Operators" by Kenneth Leroy Busbee, (Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2) is licensed under CC BY-SA 4.0
    "Operators in C | Set 1 (Arithmetic Operators)" by Unknown author, Geeks for Geeks is licensed under CC BY-SA 4.0


    This page titled 4.9: Arithmetric Operators is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.