In this tutorial, we will learn about the different types of operators in C++ Programming Language with the help of many examples.In programming, an operator is a symbol that is used to perform specific mathematical and logical computations on operands.
Types of Operator
In programming, An operator is simply a symbol that are used to perform
operations on variables and values. For Example, (+) is an operator used for
addition , (-) is an operator used for substraction , (*) is an operator
used for multiplication , (/) is an operator used for Division etc.
There are following types of operators that are used to perform different types of operations in C++ language.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operator
- Unary operator
- Ternary or Conditional Operator
- Misc Operator
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations.
Operator | Operation |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo Operation (Remainder after division) |
Addition(+)
This operator is used to add two or more value. For example,
using namespace std;
void main()
{
int x, y;
x = 7;
y = 3;
cout << "x + y = " << (x + y) << endl; // printing the sum of x and y
}
Output
Here, the + operator is used to add two variables x and y and print the result is 10
Subtraction(-)This operator is used to subtract two or more value. For example,
using namespace std;
void main()
{
int x, y;
x = 7;
y = 3;
cout << "x - y = " << (x - y) << endl; // printing the difference of x and y
}
Output
Here, the - operator is used to subtract two variables x and y and print the difference of 4
Multiplication(*)This operator is used to multiple two value. For example,
using namespace std;
void main()
{
int x, y;
x = 7;
y = 3;
cout << "x * y = " << (x * y) << endl; // printing the product of x and y
}
Output
Here, the * operator is used to multiply two variables x and y and print the result is 21.
Division(/)This operator is used to divide two value. For example,
using namespace std;
void main()
{
int x, y;
x = 7;
y = 3;
cout << "x / y = " << (x / y) << endl; // printing the division of x by y
}
Output
Here, the / operator is used to divide two variables x and y and print the result is 2
As we can see from the above example, if an integer is divided by another integer, we will get result in integer. However, if either divisor or dividend is a floating-point number, we will get the result in floating point.
7.0 / 3 is 2.5
7 / 3.0 is 2.5
7.0 / 3.0 is 2.5
The modulus operator returns the remainder of a division of one number by another. For example,
using namespace std;
void main()
{
int x, y;
x = 7;
y = 3;
cout << "x % y = " << (x % y) << endl; // // printing the modulo of x by y
}
Output
Here, The modulo operator (%) computes the remainder. When x = 7 is divided by y = 3, the remainder is 1.
Example
using namespace std;
void main()
{
int x, y;
x = 10;
y = 3;
cout << "x + y = " << (x + y) << endl; // printing the sum of x and y
cout << "x - y = " << (x - y) << endl; // printing the difference of x and y
cout << "x * y = " << (x * y) << endl; // printing the product of x and y
cout << "x / y = " << (x / y) << endl; // printing the division of x by y
cout << "x % y = " << (x % y) << endl; // printing the modulo of x by y
}
Output
x - y = 7
x * y = 30
x / y = 3
x % y = 1
0 Comments
if you have any doubt in programming concept please let me know