C++ Operator

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,

#include "iostream"
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
10

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,

#include "iostream"
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
4

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,

#include "iostream"
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
21

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,

#include "iostream"
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
2

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/3 is 2
7.0 / 3 is 2.5
7 / 3.0 is 2.5
7.0 / 3.0 is 2.5
Modulo Operator(%)

The modulus operator returns the remainder of a division of one number by another. For example,

#include "iostream"
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
1

Here, The modulo operator (%) computes the remainder. When x = 7 is divided by y = 3, the remainder is 1.


Example

#include "iostream"
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 = 13
x - y = 7
x * y = 30
x / y = 3
x % y = 1

Post a Comment

0 Comments