General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Operators

Arithmetic

Arithmetic Operators in Computer Programming

Arithmetic operators are used to perform common mathematical operations. In most programming languages, they include operators for addition, subtraction, multiplication, division, and modulo (remainder of division).

Types of Arithmetic Operators:

  1. Addition (+): Adds two operands.

    • Example: 5 + 3 results in 8.

  2. Subtraction (-): Subtracts the second operand from the first.

    • Example: 5 - 3 results in 2.

  3. Multiplication (*): Multiplies two operands.

    • Example: 5 * 3 results in 15.

  4. Division (/): Divides the first operand by the second. Note that in many programming languages, dividing two integers might result in an integer division (meaning it will truncate the decimal part).

    • Example: 5 / 2 results in 2.5 in languages with floating-point division or 2 in languages with integer division.

  5. Modulo (%): Returns the remainder of the division of the first operand by the second.

    • Example: 5 % 2 results in 1.

Examples in Programming Context:

Consider the following examples in a generic programming language:

# Example Variables
a = 10
b = 5

# Addition
sum = a + b  # sum will be 15

# Subtraction
difference = a - b  # difference will be 5

# Multiplication
product = a * b  # product will be 50

# Division
quotient = a / b  # quotient will be 2.0 (or 2 in integer division)

# Modulo
remainder = a % b  # remainder will be 0

Points to Note:

  • Division Behavior: The behavior of the division operator / can vary between programming languages. Some languages distinguish between integer division and floating-point division. For example, in Python 2, 5 / 2 equals 2, whereas in Python 3, it equals 2.5.

  • Integer Overflow: In some languages, arithmetic operations with integers can cause overflow if the result exceeds the storage capacity of the integer type.

  • Floating-Point Precision: Operations with floating-point numbers can lead to precision issues due to the way these numbers are stored in memory.

Usage:

Arithmetic operators are widely used in various programming scenarios, such as calculations in scientific computing, financial computations, basic data manipulation, and in the implementation of algorithms. Mastery of these operators is fundamental to effective programming.

PreviousOperatorsNextLogical

Last updated 1 year ago

Was this helpful?