Operators in C++ with Example: What are the 7 types of Operators?
C++ Operator: Operator is a special symbol which is used to perform logical or mathematical operation on data or variable. operand is a or variable on which the operation is to be performed.
C++, an operator is a symbol that performs a specific operation on one or more operands (the values or variables on which the operator is performed). Operators are used to manipulate and retrieve values stored in variables, as well as to perform various types of operations on them.
These operators can be used to create expressions, which can be assigned to variables or passed as arguments to functions. They can also be used in control structures such as if-else statements and loops, to control the flow of a program.
Learn Best C++ Operators All List in 2023 |
Types of Operators in C++ with Example
Arithmetic Operators in CPP
Symbols | Operations | Examples |
---|---|---|
+ | Addition | x+y |
- | Subtraction | x-y |
* | Multiplication | x*y |
/ | Division | x/y |
% | Modulus | x%y |
#include<iostream.h> #include<conio.h> void main() { clrscr(); int f,s,add,sub,mul,div,mod; cout<<"Enter First Number:"; cin>>f; cout<<"Enter second Number:"; cin>>f; add=f+s; sub=f-s; mul=f*s; div=f/s; mod=f%s; cout<<"Addition:"<<add<<endl; cout<<"Subtraction:"<<sub<<endl; cout<<"Multiplication:"<<mul<<endl; cout<<"Division:"<<div<<endl; cout<<"Modulus:"<<mod<<endl; getch(); } **********OUTPUT********** Enter First Number:5 Enter Second Number:2 Addition:7 Subtraction:3 Multiplication:10 Division:2.5 Modulus:1 |
Explain Relational Operators in C++
Symbols | Operations | Examples |
---|---|---|
= | Equal | x=y |
!= | Not Equal To | x!=y |
> | Greater Than | x>y |
< | Less Than | x<y |
>= | Greater Than or Equal To | x>=y |
<= | Less Than or Equal To | x<=y |
#include<iostream.h> #include<conio.h> int main() { int f,s; cout<<"Enter First Number:"; cin>>f; cout<<"Enter second Number:"); cin>>s; cout<<"Equal To:"<<f==s<<endl; cout<<"Greater Than:"<<f>s<<endl; cout<<"Less Than:"<<f<s<<endl; cout<<"Greater Than or Equal To:"<<f>=s<<endl; cout<<"Less Than or Equal To:"<<f<=s<<endl; cout<<"Not Equal To:"<<f!=s<<endl; } **********OUTPUT********** Enter First Number:5 Enter second Number:3 Equal To:0 Greater Than:1 Less Than:0 Greater Than or Equal To:1 Less Than or Equal To:0 Not Equal To:1 |
Logical Operators in C++ Programming Language
Symbols | Operations | Examples |
---|---|---|
&& | Logical AND | x&&y |
|| | Logical OR | x||y |
! | Logical NOT | x!y |
#include<iostream.h> #include<conio.h> int main() { int f,s; cout<<"Enter First Number:"; cin>>f; printf("Enter second Number:"; cin>>s; cout<<"Logical AND Operator:"<<f&&s<<endl; cout<<"Logical OR Operator:"<<f||s<<endl; cout<<"Logical NOT Operator:<<f!=s; } **********OUTPUT********** Enter First Number:5 Enter second Number:3 Logical AND Operator:0 Logical OR Operator:1 Logical NOT Operator:1 |
Bitwise Operators in C++ Definition
Symbols | Operations | Examples |
---|---|---|
& | Bitwise AND | x&y |
| | Bitwise OR | x|y |
^ | Bitwise XOR | x^y |
~ | Bitwise NOT | x~y |
<< | Bitwise LEFT SHIFT | x<<2 |
>> | Bitwise RIGHT SHIFT | x>>2 |
#include<iostream.h> #include<conio.h> int main() { int f,s; cout<<"Enter First Number:"; cin>>f; cout<<"Enter second Number:"; cin>>s; cout<<"Bitwise AND Operator:<<"f&s<<endl; cout<<"Bitwise OR Operator:"<<f|s<<endl; cout<<"Bitwise XOR Operator:"<<f^s<<endl; cout<<"Bitwise NOT Operator:"<<f~s<<endl; cout<<"Bitwise LEFT SHIFT Operator:"<<f<<1<<endl; cout<<"Bitwise RIGHT SHIFT Operator:"<<f>>1; } **********OUTPUT********** Enter First Number:10 Enter second Number:5 Bitwise AND Operator:0 Bitwise OR Operator:15 Bitwise XOR Operator:15 Bitwise NOT Operator:-11 Bitwise LEFT SHIFT Operator:20 Bitwise RIGHT SHIFT Operator:5 |
Assignment Operator C++ Implementation
Symbols | Operations | Examples |
---|---|---|
+= | Addition | x+=y or x=x+y |
-= | Subtraction | x-=y or x=x+y |
*= | Multiplication | x*=y or x=x+y |
/= | Division | x/=y or x=x+y |
%= | Modulus | x%=y or x=x+y |
#include<iostream.h> #include<conio.h> void main() { int a; cout<<"Enter First Number:"; cin>>a; cout<<"Assignment Addition:"<<a+=5<<endl; cout<<"Assignment Subtraction:"<<a-=5<<endl; cout<<"Assignment Multiplication:"<<a*=5<<endl; cout<<"Assignment Division:"<<a/=5<<endl; cout<<"Assignment Modulus:"<<a%=5; } getch(); } **********OUTPUT********** Enter First Number:7 Assignment Addition:12 Assignment Subtraction:7 Assignment Multiplication:35 Assignment Division:7 Assignment Modulus:2 |
Increment Decrement Operator Program in C++
Symbols | Operations | Examples |
---|---|---|
++ | Pre-Increment | x++ |
-- | Pre-Decrement | x-- |
++ | Post-Increment | ++x |
-- | Post-Decrement | --x |
#include <iostream.h> #include<conio.h> int main() { int x = 10; x++; cout<<"x is:<<x<<endl; ++x; cout<<"x is:"<<x; return 0; } **********OUTPUT********** x is 11 x is 12 |
#include <iostream.h> #include<conio.h> int main() { int x = 10; int y = 5; int z = x++ + y; int a = x-- + y; int b = ++x + y; int c = --x + y; return 0; } **********OUTPUT********** z is 15 a is 16 b is 16 c is 15 |
Use of Conditional Operator in C++
#include <iostream.h> #include<conio.h> int main() { int x = 10 int y=20; x>y?cout<<"x is greater than b":cout<<"b is greater than a"; } **********OUTPUT********** b is greater than a |
Special Operators in C++ with Examples
Symbols | Operations | Examples |
---|---|---|
& | it is used find address | int a=10; cout<<&a; |
* | It is used pointer variable | int *p; |
sizeof() | It Used Memory Size | int a=10; cout<<sizeof(a); |
0 Comments