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:
- Unary Arithmetic Operator
- 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++
#include <iostream>
using namespace std;
int main()
{
int GFG1, GFG2;
GFG1 = 10;
GFG2 = 3;
cout<< "GFG1 + GFG2= " << (GFG1 + GFG2) << endl;
cout << "GFG1 - GFG2 = " << (GFG1 - GFG2) << endl;
cout << "GFG1 * GFG2 = " << (GFG1 * GFG2) << endl;
cout << "GFG1 / GFG2 = " << (GFG1 / GFG2) << endl;
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++
#include <iostream>
using namespace std;
int main()
{
int x = 5;
cout << "x++ is " << x++ << endl;
cout << "++x is " << ++x << endl;
int y = 10;
cout << "y-- is " << y-- << endl;
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.
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...