Skip to main content
Engineering LibreTexts

2.15: Arithmetic Operators

  • Page ID
    10607
  • \( \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}}\)

    Overview

    The basic arithmetic operations are addition, subtraction, multiplication, and division. Arithmetic is performed according to an order of operations.[1]

    Discussion

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

    Action Common 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, may act differently. Please refer to the next section on “Integer Division and Modulus”.

    Arithmetic Assignment Operators

    Many programming languages support a combination of the assignment (=) and arithmetic operators (+, -, *, /, %). Various textbooks call them “compound assignment operators” or “combined assignment operators”. Their usage can be explained in terms of the assignment operator and the arithmetic operators. In the table, we will use the variable age and you can assume that it is of integer data type.

    Arithmetic assignment examples: Equivalent code:
    age += 14; age = age + 14;
    age -= 14; age = age - 14;
    age *= 14; age = age * 14;
    age /= 14; age = age / 14;
    age %= 14; age = age % 14;

    Pseudocode

    Function Main
        ... This program demonstrates arithmetic operations.
        Declare Integer a
        Declare Integer b
        
        Assign a = 3
        Assign b = 2
        Output "a = " & a
        Output "b = " & b
        Output "a + b = " & a + b
        Output "a - b = " & a - b
        Output "a * b = " & a * b
        Output "a / b = " & a / b
        Output "a % b = " & a % b
    End
    

    Output

    a = 3
    b = 2
    a + b = 5
    a - b = 1
    a * b = 6
    a / b = 1.5
    a % b = 1
    

    Flowchart

    333px-Flowgorithm_Arithmetic.svg_.png


    2.15: Arithmetic Operators is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?