Arithmetic Operators in C++ Programming
Operators in C++
In this lesson, we are going to look at what is arithmetic operators and will learn the different types of arithmetic operator with the help of examples.
What is Arithmetic Operators
Arithmetic Operators in C++ are used to performing arithmetic or mathematical operations on operands.
Arithmetic Operators are of two types:
- Unary Operators - Operators that operates or works with a single operand are called unary operators. For example: (++ , --).
- Binary Operators - Operators that operates or works with two operands are called binary operators. For example: (+, –, *, /, %).
Unary Operators
There are 2 types of Unary operators:
- Increment Operator ++
- Decrement Operator --
Increment Operator ++
The Increment Operator is used to increased the value of the variable by 1.
There are 2 types of Increment Operators and they are:
- Pre-Increment
- Post-Increment
Pre-Increment
The Pre-Increment operator is used to increase the value of a variable by 1 before using it in an expression. In the Pre-Increment, value is first increased and then used inside the expression. Let's see the example for more understanding.
Example
int x=5, y;
y=++x; // value of x is increase by 1 and then stored in y
In the example above the value of x which is 5 is first increase by 1 and then stored in x. So the result of y is 6.
Post-Increment
The Post-Increment operator is used to increase the value of the variable by 1 after executing the expression completely in which post-increment is used. In the Post-Increment, value is first used in an expression and then increased by 1. Let's see the example for more understanding.
Example
int x=5, y;
y=x++; // value of x is stored in y first and after that increase by 1
In the example above the value of x which is 5 is first stored in y and after that increased by 1. So the result of y is 5 and the value of x becomes 6.
Decrement Operator --
Decrement Operator is used to decrease the value of the variable by 1.
There are 2 types of Decrement Operators and they are:
- Pre-Decrement
- Post-Decrement
Pre-Decrement
The Pre-Decrement operator is used to decrease the value of a variable by 1 before using it in an expression. In the Pre-Decrement, value is first decreased by 1 and then used inside the expression. Let's see the example for more understanding.
Example
int x=5, y;
y=--x; // value of x is decrease by 1 and then stored in y
In the example above the value of x which is 5 is first decrease by 1 and then stored in x. So the result of y is 4.
Post-Decrement
The Post-Decrement operator is used to decrease the value of the variable by 1 after executing the expression completely in which post-decrement is used. In the Post-Decrement, value is first used in an expression and then decreased by 1. Let's see the example for more understanding.
Example
int x=5, y;
y=x--; // value of x is stored in y first and after that decrease by 1
In the example above the value of x which is 5 is first stored in y and after that decreased by 1. So the result of y is 5 and the value of x becomes 4.
Binary Operators
There are 5 types of binary operators:
- + Addition
- - Subtraction
- * Multiplication
- / Division
- % Modulus
+ Addition Operator
+ Addition Operator is used for adding the value of two or more variables. Let's see the example for more understanding.
Example
int x=5, y=2, z;
z=x+y; // value of z becomes 7
In the example above the value of x and y is first added and then stored in z. So the result of z is 7.
- Subtraction Operator
- Subtraction Operator is used for subtracting the value of two or more variables. Let's see the example for more understanding.
Example
int x=5, y=2, z;
z=x-y; // value of z becomes 3
In the example above the value of y is subtracted from x and the final result is stored in z. So the result of z is 3.
* Multiplication Operator
* Multiplication Operator is used for multiplying the value of two or more variables. Let's see the example for more understanding.
Example
int x=5, y=2, z;
z=x*y; // value of z becomes 10
In the example above the value of x and y is first multiplied and then stored in z. So the result of z is 10.
/ Division Operator
/ Division Operator is used for dividing the value of two or more variables and the quotient is returned as the result of the division. Let's see the example for more understanding.
Example
int x=5, y=2, z;
z=x/y; // value of z becomes 2 which is a quotient
In the example above the value of x and y is first divided and then stored the quotient in z. So the result of z is 2. Here x and y both are integer variable so the result of their division will be in integer only.
% Modulus Operator
% Modulus Operator is used for dividing the value of two or more variables and the remainder is returned as the result of the division. Let's see the example for more understanding.
Example
int x=5, y=2, z;
z=x%y; // value of z becomes 1 which is a remainder
In the example above the value of x and y is first divided and then stored the remainder in z. So the result of z is 1.
Test Your Knowledge
Attempt the multiple choice quiz to check if the lesson is adequately clear to you.