The Wayback Machine - https://web.archive.org/web/20241128003954/https://www.geeksforgeeks.org/cpp-arithmetic-operators/
Open In App

C++ Arithmetic Operators

Last Updated : 15 Sep, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands. For example, ‘+’ is used for addition, ‘–‘ is used for subtraction,  â€˜*’ is used for multiplication, etc. In simple terms, arithmetic operators are used to perform arithmetic operations on variables and data; they follow the same relationship between an operator and an operand.

C++ Arithmetic operators are of 2 types:

  1. Unary Arithmetic Operator
  2. Binary Arithmetic Operator

1. Binary Arithmetic Operator

These operators operate or work with two operands. C++ provides 5 Binary Arithmetic Operators for performing arithmetic functions:

Operator

Name of the Operators

Operation

Implementation

+

Addition

Used in calculating the Addition of two operands

x+y

–

Subtraction

Used in calculating Subtraction of two operands

x-y

*

Multiplication

Used in calculating Multiplication of two operands

x*y

/

Division

Used in calculating Division of two operands

x/y

%

Modulus

Used in calculating Remainder after calculation of two operands

x%y

Example:

C++




// C++ program to execute all 5
// arithmetic function
#include <iostream>
using namespace std;
  
int main()
{
    int GFG1, GFG2;
    GFG1 = 10;
    GFG2 = 3;
  
    // printing the sum of GFG1 and GFG2
    cout<< "GFG1 + GFG2= " << (GFG1 + GFG2) << endl;
  
    // printing the difference of GFG1 and GFG2
    cout << "GFG1 - GFG2 = " << (GFG1 - GFG2) << endl;
  
    // printing the product of GFG1 and GFG2
    cout << "GFG1 * GFG2 = " << (GFG1 * GFG2) << endl;
  
    // printing the division of GFG1 by GFG2
    cout << "GFG1 / GFG2 = " << (GFG1 / GFG2) << endl;
  
    // printing the modulo of GFG1 by GFG2
    cout << "GFG1 % GFG2 = " << (GFG1 % GFG2) << endl;
  
    return 0;
}


Output

GFG1 + GFG2= 13
GFG1 - GFG2 = 7
GFG1 * GFG2 = 30
GFG1 / GFG2 = 3
GFG1 % GFG2 = 1

2. Unary Operator

These operators operate or work with a single operand.
 

Operator Symbol Operation Implementation
Decrement Operator — Decreases the integer value of the variable by one –x or x —
Increment Operator ++ Increases the integer value of the variable by one ++x or x++

Example:

C++




// C++ Program to demonstrate the 
// increment and decrement operators
#include <iostream>
using namespace std;
  
int main()
{
    int x = 5;
  
    // This statement Incremented 1
    cout << "x++ is " << x++ << endl;
  
    // This statement Incremented 1 
    // from already Incremented
    // statement resulting in 
    // Incrementing of 2
    cout << "++x is " << ++x << endl;
  
    int y = 10;
    
    // This statement Decremented 1
    cout << "y-- is " << y-- << endl;
  
    // This statement Decremented 1
    // from already Decremented
    // statement resulting in 
    // Decrementing of 2
    cout << "--y is " << --y << endl;
  
    return 0;
}


Output

x++ is 5
++x is 7
y-- is 10
--y is 8

In ++x, the variable’s value is first increased/incremented before being utilised in the program.

In x++, a variable’s value is assigned before it is increased/incremented.

Similarly happens for the decrement operator.



Previous Article
Next Article

Similar Reads

How to sum two integers without using arithmetic operators in C/C++?
Given two integers a and b, how can we evaluate the sum a + b without using operators such as +, -, ++, --, ...? Method 1 (Using pointers)An interesting way would be: C/C++ Code // May not work with C++ compilers and // may produce warnings in C. // Returns sum of 'a' and 'b' int sum(int a, int b) { char *p = a; return (int)&amp;p[b]; } Despite
4 min read
Conditionally assign a value without using conditional and arithmetic operators
Given 4 integers a, b, y, and x, where x can assume the values of either 0 or 1 only. The following question is asked: If 'x' is 0, Assign value 'a' to variable 'y' Else (If 'x' is 1) Assign value 'b' to variable 'y'. Note: - You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator (+, -, *, /)
6 min read
Sum of array using pointer arithmetic
Given an array, write a program to find the sum of array using pointers arithmetic. In this program we make use of * operator . The * (asterisk) operator denotes the value of variable. The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the value of the memory location pointed by the pointer . sum() functi
2 min read
Arithmetic operations with std::bitset in C++
A bitset is an array of boolean values, but each boolean value is not stored separately. Instead, bitset optimizes the space such that each bool takes 1-bit space only, so space taken by bitset say, bs is less than that of bool bs[N] and vector<bool> bs(N). However, a limitation of bitset is, N must be known at compile-time, i.e., a constant
3 min read
Character Arithmetic in C
As already known character range is between -128 to 127 or 0 to 255. This point has to be kept in mind while doing character arithmetic. What is Character Arithmetic?Character arithmetic is used to implement arithmetic operations like addition, subtraction, multiplication, and division on characters in C language. In character arithmetic character
2 min read
Multidimensional Pointer Arithmetic in C/C++
In C/C++, arrays and pointers have similar semantics, except on type information. As an example, given a 3D array int buffer[5][7][6]; An element at location [2][1][2] can be accessed as "buffer[2][1][2]" or *( *( *(buffer + 2) + 1) + 2). Observe the following declaration T *p; // p is a pointer to an object of type T When a pointer p is pointing t
4 min read
Ratio Manipulations in C++ | Set 1 (Arithmetic)
C++ allows us to perform addition, subtraction, multiplication, and division operations on fractions. One method of adding ratios is discussed in the following article - Program to add two fractions. The method used here is tedious and lengthy, so to overcome that a better method was introduced in C++. The <ratio> header file in C++ allows us
3 min read
Character Arithmetic in C++
Character arithmetic in C++ involves performing mathematical operations on characters. In C++, characters are represented using the char data type, which is essentially an integer type that stores ASCII values. In this article, we will discuss character arithmetic and how to perform character arithmetic in C++. What is Character Arithmetic in C++?C
2 min read
C++ Pointer Arithmetic
In C++, pointer variables are used to store the addresses of other variables, functions, structures, and even other pointers and we can use these pointers to access and manipulate the data stored at that address. Pointer arithmetic means performing arithmetic operations on pointers. It refers to the operations that are valid to perform on pointers.
8 min read
reinterpret_cast in C++ | Type Casting operators
reinterpret_cast is a type of casting operator used in C++. It is used to convert a pointer of some data type into a pointer of another data type, even if the data types before and after conversion are different.It does not check if the pointer type and data pointed by the pointer is same or not. Syntax : data_type *var_name = reinterpret_cast <
3 min read
unordered_set operators in C++ STL
Unordered_set provides two operators in C++ STL. These are: Syntax: 1. (unordered_set &lhs == unordered_set &rhs) 2. (unordered_set &lhs != unordered_set &rhs) These operators are discussed in detail below: unordered_set == operator in C++ STL The ‘==’ is an operator in C++ STL performs equality comparison operation between two unor
5 min read
C++ Pointer Operators
Prerequisite: Pointers in C++ A pointer variable is a variable that stores the address of another variable or in other a pointer variable points to the variable whose address is stored inside it. Syntax: int *pointer_name; There are mainly two types of Pointer operators mainly used: Address of operator (&)The indirection operator/Dereference op
2 min read
Input/Output Operators Overloading in C++
Operator Overloading is a part of Polymorphism, which enables the feature because of which we can directly use operators with user-defined classes and objects. To read more about this, refer to the article operator overloading in C++. Input/Output Operators(>>/<<) Overloading in C++ We can't directly use the Input/Output Operators (>
2 min read
C++ Increment and Decrement Operators
Prerequisite: Operators in C++ What is a C++ increment Operator? The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only. There are two types of C++ increment Operator: Pre-Increm
4 min read
C++ Comparison Operators
Comparison operators are operators used for comparing two elements, these are mostly used with if-else conditions as they return true-false as result. There are mainly 6 Comparison Operators namely: Greater than (>) : this operator checks whether operand1 is greater than operand2. If the result turns out to be true, it returns true or else retur
3 min read
C++ Relational Operators
In C++ programming language, we sometimes require to compare values and expressions. This comparison allows us to determine relationships, make decisions, and control the flow of our programs. The relational operators in C++ provide the means to compare values and evaluate conditions. In this article, we will learn about C++ relational operators an
4 min read
C++ Logical Operators
In C++ programming languages, logical operators are symbols that allow you to combine or modify conditions to make logical evaluations. They are used to perform logical operations on boolean values (true or false). In C++, there are three logical operators: Logical AND ( && ) OperatorLogical OR ( || ) OperatorLogical NOT ( ! ) OperatorLet's
4 min read
Unary Operators In C++
Unary operators in C++ are those operators that work on a single value (operand). They perform operations like changing a value's sign, incrementing or decrementing it by one, or obtaining its address. Examples include ++ for increment, -- for decrement, + for positive values, - for negative values,! for logical negation, and & for retrieving m
8 min read
Assignment Operators In C++
In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other wor
7 min read
What are the Operators that Can be and Cannot be Overloaded in C++?
There are various ways to overload Operators in C++ by implementing any of the following types of functions: 1) Member Function 2) Non-Member Function 3) Friend Function List of operators that can be overloaded are: + - * ? % ? & | ~ ! = < > += -= *= ?= %= ?= &= |= << >> <<= >>= == != <= >= && ||
4 min read
Increment (Decrement) operators require L-value Expression
What will be the output of the following program? #include<stdio.h> int main() { int i = 10; printf("%d", ++(-i)); return 0; } A) 11 B) 10 C) -9 D) None Answer: D, None - Compilation Error. Explanation: In C/C++ the pre-increment (decrement) and the post-increment (decrement) operators require an L-value expression as operand. Provi
1 min read
Order of operands for logical operators
The order of operands of logical operators &&, || are important in C/C++. In mathematics, logical AND, OR, etc... operations are commutative. The result will not change even if we swap RHS and LHS of the operator. In C/C++ (may be in other languages as well)  even though these operators are commutative, their order is critical. For example
1 min read
const_cast in C++ | Type Casting operators
C++ supports following 4 types of casting operators: 1. const_cast 2. static_cast 3. dynamic_cast 4. reinterpret_cast 1. const_cast const_cast is used to cast away the constness of variables. Following are some interesting facts about const_cast. 1) const_cast can be used to change non-const class members inside a const member function. Consider th
4 min read
Overloading stream insertion (<>) operators in C++
In C++, stream insertion operator "<<" is used for output and extraction operator ">>" is used for input. We must know the following things before we start overloading these operators. 1) cout is an object of ostream class and cin is an object of istream class 2) These operators must be overloaded as a global function. And if we want to
2 min read
Written version of Logical operators in C++
Can we use keywords in place of operators in C++ ? Yes, certainly, we can. The ANSI C++ Standard has proposed keywords for several C++ operators . They originated in C in the header at the time when there were keyboards that couldn't type the required symbols like &&, !, || etc. In C++, they became alternate token just like regular tokens, except f
2 min read
Comparing String objects using Relational Operators in C++
If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered. Parameters : Two Strings required t
2 min read
Casting Operators in C++
Casting operators are used for type casting in C++. They are used to convert one data type to another. C++ supports four types of casts: static_castdynamic_castconst_castreinterpret_cast1. static_castThe static_cast operator is the most commonly used casting operator in C++. It performs compile-time type conversion and is mainly used for explicit c
5 min read
Bitwise Operators in C++
There are various Operators present in C++. Every Operator has a particular symbol as well as an Operation to perform. We have various categories of operators in C++. Arithmetic OperatorsRelational OperatorsLogical OperatorsAssignment OperatorsBitwise OperatorsIn this article, we will learn about the Bitwise Operators in C++. C++ Bitwise OperatorsB
6 min read
Operators in C++
An operator is a symbol that operates on a value to perform specific mathematical or logical computations. They form the foundation of any programming language. In C++, we have built-in operators to provide the required functionality. An operator operates the operands. For example,  int c = a + b;Here, '+' is the addition operator. 'a' and 'b' are
13 min read
Conversion Operators in C++
In C++, the programmer abstracts real-world objects using classes as concrete types. Sometimes, it is required to convert one concrete type to another concrete type or primitive type implicitly. Conversion operators play an important role in such situations. It is similar to the operator overloading function in class. In this article, we will learn
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg