![]() |
How To Use C++ Operators With Examples |
How To Use C++ Operators With Examples
Operator C++ Language: A C++ Language is a Special operator that is a mathematical operation performed in the C++ language.
Operator Can be used to solve the mathematical operation Perform logically. The operator has used variables and data calculation.
Types Operator in C++ Language
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Assignment operator
5. Bitwise Operator
6. Increment operator
7. Decrement Operator
8. Conditional Operator
Important Same Other Languages Operator
👉C Operators
👉Java Operators
👉Python Operators
Example of Arithmetic operator in C Language
Here a is operands value (+) Addition is operator and b is operands. It can be used for additional operations performed.
2.Subtraction:a-b or 20-10=10
Here a is operands value (-) Subtraction sign is operator and b is operands. It can be used as Subtraction operations performed.
3.Multiplication:a*b or 10*10=100
Here a is operands value (*) Multiplication sign is operator and b is operands. It can be used Multiplication operations performed.
4.Division:a/b or 10/5=2
Here a is operands value (/) Division sign operator and b is operands. It can be used for Division operations performed.
5.Modulas:a%b or 10%3=1
Here a is operands value (%) Modules sign operator and b is operands. It can be used Modules operations performed.
Arithmetic Operator Table
Sign. Operation. Example
(+) Addition (a+b)
(-) Subtraction (a-b)
(*) Multiplication (a*b)
(/) Division (a/b)
(%) Modules (a%b)
Sign. Operation. Example
(+) Addition (a+b)
(-) Subtraction (a-b)
(*) Multiplication (a*b)
(/) Division (a/b)
(%) Modules (a%b)
How To Use Arithmetic Operator
#include<iostream>
using namespace std;
int main()
{
int x=35
int y=6;
int add,sub,multi,div,mod;
add=x+y;
sub=x-y;
multi=x*y
div=x/y;
mod=x%y;
cout<<"Addition:"<<add<<"\n;
cout<<"Subtraction:"<<sub<<"\n;
cout<<"Multiplication:"<<multi<<"\n;
cout<<"Division:"<<div<<"\n;
cout<<"Modulus:"<<mod<<"\n;
}
*****OUTPUT*****
Addition:41
Subtraction:29
Multiplication:210
Division:5.8333333
Modulus:5
#include<iostream>
using namespace std;
{
int x=35
int y=6;
int add,sub,multi,div,mod;
add=x+y;
sub=x-y;
multi=x*y
div=x/y;
mod=x%y;
cout<<"Addition:"<<add<<"\n;
cout<<"Subtraction:"<<sub<<"\n;
cout<<"Multiplication:"<<multi<<"\n;
cout<<"Division:"<<div<<"\n;
cout<<"Modulus:"<<mod<<"\n;
}
*****OUTPUT*****
Addition:41
Subtraction:29
Multiplication:210
Division:5.8333333
Modulus:5
Relational Operator in C++ Language
Relational Operator Table
Sign. Operation. Example
(==) Equal To (a+b)
(!=) Not Equal To (a-b)
(>) Greater Than (a*b)
(<) Less Than (a/b)
(>=) Grater than Or Equal (a%b)
(<=) Less Than Or Equal (a%b)
Sign. Operation. Example
(==) Equal To (a+b)
(!=) Not Equal To (a-b)
(>) Greater Than (a*b)
(<) Less Than (a/b)
(>=) Grater than Or Equal (a%b)
(<=) Less Than Or Equal (a%b)
How To Use Relational Operator in C++?
#include<iostream>
using namespace std;
int main()
{
int a=10;
int b=10;
if(a==b)
{
cout<<"value of a and b are equal"<<"\n";
}
if(a!=100)
{
cout<<"value not equal to"<<"\n";
}
if(a>5)
{
cout<<"value a is grater than 5"<<"\n";
}
if(a<50)
{
cout<<"value a is less than 50"<<"\n";
}
if(a>=10)
{
cout<<"value a is grater than or equal to 10"<<"\n";
}
if(a<=10)
{
cout<<"value a is less than or equal to 10"<<"\n";
}
}
*****OUTPUT*****
value of a and b are equal
value not equal to
value a is greater than 5
value a is less than 50
value a is greater than or equal to 10
value a is less than or equal to 10
#include<iostream>
using namespace std;
int main()
{
int a=10;
int b=10;
if(a==b)
{
cout<<"value of a and b are equal"<<"\n";
}
if(a!=100)
{
cout<<"value not equal to"<<"\n";
}
if(a>5)
{
cout<<"value a is grater than 5"<<"\n";
}
if(a<50)
{
cout<<"value a is less than 50"<<"\n";
}
if(a>=10)
{
cout<<"value a is grater than or equal to 10"<<"\n";
}
if(a<=10)
{
cout<<"value a is less than or equal to 10"<<"\n";
}
}
*****OUTPUT*****
value of a and b are equal
value not equal to
value a is greater than 5
value a is less than 50
value a is greater than or equal to 10
value a is less than or equal to 10
Logical Operator in C++ Language
Types of Logical Operator
1. AND Operator can be used decisions are two values.AND Operator two values are True AND Operator return True.
2. OR operator performs a logical condition on two expressions. if or Operator can be two expressions are True OR Operator return true.
3. NOT operator is very easy Operator. it can be used condition are True NOT Operator returns False and condition are False NOT Operator returns True.
How To Use Logical AND Operator in C++?
#include<iostream>
#include<string.h>
using namespace std;
int main()
{ char user[100]="icoderweb";
char pass[100]="123456";
char user1[100];
char pass1[100];
cout<<"Enter username:";
cin>>user1;
cout<<"Enter password:";
cin>>pass1;
if(strcmp(user,user1)==0&&strcmp(pass,pass1)==0)
{
cout<<"Login successfully";
}
else
{
cout<<"invalid username and password";
}
}
*****OUTPUT*****
Enter username:icoderweb
Enter password:123456
Login successfully
#include<iostream>
#include<string.h>
using namespace std;
*****OUTPUT*****
Enter username:icoderweb
Enter password:123456
Login successfully
How To Use Logical OR Operator in C++?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ char user[100]="icoderweb";
char pass[100]="123456";
char user1[100];
char pass1[100];
cout<<"Enter username:";
cin>>user1;
cout<<"Enter password:";
cin>>pass1;
if(strcmp(user,user1)==0||strcmp(pass,pass1)==0)
{
cout<<"Login successfully";
}
else
{
cout<<"invalid username and password";
}
}
*****OUTPUT*****
Enter username:icoderweb
Enter password:123653265
Login successfully
#include<iostream>
#include<conio.h>
using namespace std;
*****OUTPUT*****
Enter username:icoderweb
Enter password:123653265
Login successfully
How To Use NOT Operator in C++?
#include<iostream>
using namespace std;
int main()
{ int a=100;
cout<<"Not Operator value:"<<,!(a!=100);
return 0;
}
*****OUTPUT*****
Not Operator value:1
#include<iostream>
using namespace std;
Not Operator value:1
Increment Operator in C++ Language
How To Use Increment Operator in C++?
#include<iostream>
using namespace std;
int main()
{
int a=5;
int b=10;
cout<<"pre-increment operator:"<<++a<<"\n;
cout<<"post-increment operator:<<b+1<<"\n;
}
*****OUTPUT*****
pre-increment operator:6
post-increment operator:11
#include<iostream>
using namespace std;
*****OUTPUT*****
pre-increment operator:6
post-increment operator:11
Decrement Operator in C++ Language
How To Use Decrement Operator in C++?
#include<iostream>
using namespace std;
int main()
{
int a=5;
int b=10;
cout<<"pre-decrement operator:"<<--a<<"\n;
cout<<"post-decrement operator:"<<b-1<<"\n;
}
*****OUTPUT*****
pre-decrement operator:4
post-decrement operator:9
#include<iostream>
using namespace std;
pre-decrement operator:4
post-decrement operator:9
Bitwise Operator in C++ Language
Bitwise Operator: A Bitwise operator is an operation performed on the data bit level. the bitwise operator is also shifted bit right to left.
Note: Bitwise operators are not allowed to float and double.
Bitwise Operator Table
Sign. Operation. Example
(&) Bitwise AND (a&b)
(|) Bitwise OR (a|b)
(<<) Bitwise LEFT (a<<2)
(>>) Bitwise RIGHT (a>>2)
Sign. Operation. Example
(&) Bitwise AND (a&b)
(|) Bitwise OR (a|b)
(<<) Bitwise LEFT (a<<2)
(>>) Bitwise RIGHT (a>>2)
How To Use Bitwise Operator in C++?
#include <iostream>
using namespace std;
int main()
{
int y=10, x=30;
cout<<"Bitwise AND Operator:"<<(x&y)<<"\n";
cout<<"Bitwise OR Operator:"<<(x|y)<<"\n";
cout<<"Right shift by:"<<(x>>2)<<"\n";
cout<<"Left shift by:"<<(x<<2);
return 0;
}
*****OUTPUT*****
Bitwise AND Operator:10
Bitwise OR Operator:30
Right shift by:7
Left shift by:120
Bitwise AND Operator:10
Bitwise OR Operator:30
Right shift by:7
Left shift by:120
Assignment Operator Table
Sign. Operation Example
(=) a=b (a=b)
(+=) a+=b (a=a+b)
(-=) a-=b (a=a-b)
(*=) a*=b (a=a*b)
(/=) a/=b (a=a/b)
(%=) a%=b (a=a%b)
Sign. Operation Example
(=) a=b (a=b)
(+=) a+=b (a=a+b)
(-=) a-=b (a=a-b)
(*=) a*=b (a=a*b)
(/=) a/=b (a=a/b)
(%=) a%=b (a=a%b)
How To Use Assignment Operator in C++?
#include <iostream>
using namespace std;
int main()
{
int a=10,b=5;
a+=b;
cout<<"Assignment Addition:"<<a<<"\n;
int a1=10,b1=5;
a1-=b1;
cout<<"Assignment Subtraction:"<<a1<<"\n;
int a2=10,b2=5;
a2*=b2;
cout<<("Assignment Multiplication:"<<a2<<"\n;
int a3=10,b3=5;
a3/=b3;
cout<<"Assignment Division:"<<a3<<"\n;
int a4=16,b4=5;
a4%=b4;
cout<<"Assignment Moduls:"<<a4<<"\n";
return 0;
}
*****OUTPUT*****
Assignment Addition:15
Assignment Subtraction:5
Assignment Multiplication:50
Assignment Division:2
Assignment Moduls:1
#include <iostream>
using namespace std;
int main()
{
int a=10,b=5;
a+=b;
cout<<"Assignment Addition:"<<a<<"\n;
int a1=10,b1=5;
a1-=b1;
cout<<"Assignment Subtraction:"<<a1<<"\n;
int a2=10,b2=5;
a2*=b2;
cout<<("Assignment Multiplication:"<<a2<<"\n;
int a3=10,b3=5;
a3/=b3;
cout<<"Assignment Division:"<<a3<<"\n;
int a4=16,b4=5;
a4%=b4;
cout<<"Assignment Moduls:"<<a4<<"\n";
return 0;
}
*****OUTPUT*****
Assignment Addition:15
Assignment Subtraction:5
Assignment Multiplication:50
Assignment Division:2
Assignment Moduls:1