C++ Programming Operators
An
operator is a symbol that operates on a
value or a variable. For example + is an
operator to perform addition.
C
has a wide range of operators to perform
various operations.
1 . Arithmetical Operator
2. Relation Operator
3. Increment / Decrement Operator
4 . Logical Operation
5. Sizeof Operator
6. Assignment Operator
7. Comma operator
Fig: C++ Programming Operators |
1.
Arithmetical Operator
An arithmetical operator perform mathematical
operations such as addition,
subtraction, multiplication, division etc on a numerical values(constants and
variables).
Example:
I.
a+b=addition
II.
a-b=subtraction
III.
a*b=multiplication
IV.
a/b=division
V.
a%b=modulo
division
Program of Arithmetical Operator
#include<iostream.h>
int
main()
{
int
a,b,c,d,e,f,g;
cout<<”input
two number”;
cin>>a>>b;
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
cout<<c<<d<<e<<f<<g;
return
0;
}
Output:
Input
two number:
20
10
30 10
20 0 20
2.
Relational Operator
A relational operator check the relationship between
two operands. If the relation is true, it return 1; if the relation is false,
it returns value 0.
Example:
I.
== Equal to 5==3 is evaluated to
0
II.
> greater than 5>3 is evaluated to 1
III.
< less than 5<3 is evaluated
to 0
IV.
!= Not equal to 5!=3 is evaluated to 1
V.
>= greater than or equal to 5==3 is evaluated to 1
VI.
<= less than
or equal to 5==3 is
evaluated to 0
Program of Relational Operator
#include<iostream.h>
int
main()
{
int a,b,c,d,e,f,g,h;
cout<<enter
number”;
cin>>a>>b;
c=a>b;
d=a<b;
e=a>=10;
f=a<=20;
g=a==50;
h=a!=50;
Output:
5==5
is 1
5>5
is 0
5<5
is 0
5
!=5 is 0
5>=5
is 1
5<=5
is 1
3.
Increment / Decrement Operators
C programming has two operators increment ++ and decrement
– to change the value of and operand by 1.
Increment ++
increase the value by 1 when decrement -- decrease the value by 1.These two
operators are unary operators, meaning they only operate on a single value.
Program of Increment / Decrement Operator
#include<iostream.h>
int main()
{
int
a=10,b=100;
cout<<”enter number”;
cin>>a>>b;
a++;
b--;
cout<<a<<b;
return0;
}
Output:
Enter number
++a=11
--b=99
4.Logical Operator
An
expression containing logical operator return either 0 or 1 depending upon
whether expression result true or false.
Example:
I.
&& AND
II.
|| OR
III.
! NOT
Program of Logical Operator
#include<iostream.h>
int
main()
{
int a=5,
b=5, c=10, result;
result=(a==b)&&(c>b);
cout<<“(a==b)&&(c>b)
is %d \n “, result;
result=(a==b)&&(c<b);
cout<<“(a==b)&&(c<b)
is %d \n “, result;
result=(a==b)
|| (c<b);
cout<<“(a==b)
|| (c<b) is %d \n “, result;
result=(a
!=b) || (c<b);
cout<<“(a!=b)
|| (c<b) is %d \n “, result;
result=!(a!=b);
cout<<“!(a==b)
is %d \n”,result;
result=!(a==b);
cout<<“!(a==b)
is %d \n”,result;
return
0;
}
Output:
(a==b)
&& (c>b) is 1
(a==b)
&&(c<b) is 0
(a==b)
|| (c<b) is 1
(a!=b)
|| (c<b) is 0
!(a!=b)
is 1
!(a==b)
is 0
5.Sizeof Operator
The
sizeof is a unary operator that return the size of data.
Program of Sizeof Operator
#include<iostream.h>
int
main()
{
int a;
float b;
double c;
char d;
cout<<“size
of int=%lu byte\n”,sizeof(a);
cout<<“size
of float=%lu byte\n”,sizeof(b);
cout<<“size
of double=%lu byte\n”,sizeof(c);
cout<<“size
of char=%lu byte\n”,sizeof(d);
return
0;
}
Output:
Size
of int=4 byte
Size
of float=4 byte
Size
of double=8 byte
Size
of char=1 byte
6. Assignment Operator
An
assignment operator is used for assigning a value to a variable. The most
common assignment operator is =.
Example:
I.
= a=b a=b
II.
+= a+=b a= a+b
III.
-= a-=b a=a-b
IV.
*= a*=b a=a*b
V.
/= a/=b a=a/b
VI.
%= a%=b a=a%b
Program of Assignment Operator
#include<iostream.h>
int
main()
{
int a=5,
c;
c=a;
cout<<c;
c +=a;
cout<<c;
c -=a;
cout<<c;
c
*=a;
cout<<c;
c /=a;
cout<<c;
c
%=a;
cout<<c;
return
0;
}
Output:
C=5
C=10
C=5
C=25
C=5
C=0
8.
7. Comma Operator
Comma operator are used to link related expression
together.
For Example:
int a, c=5,
d;
No comments:
Post a Comment