Operators:
Operator is symbol and it is
used to specific task .
C++ is supported to following
types of operators.
- Arithmetical Operators
- Relational Operators
- Comma Operator
- Assignment Operator
- Conditional Operators
- Logical Operators
- Bitwise Operators
- Increment And Decrement Operators
- Unary Operators
- Binary Operators
1.Arithmetical Operators:
+ , - , *, / ,% are called Arithmetical Operators.
+ , - , *, / ,% are called Arithmetical Operators.
Ex: Write a Program of sum
of two Numbers
Program-1
|
Program-2
|
Program-3
|
Program-4
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
int b;
int c;
clrscr();
a=20;
b=10;
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
c=a+b;
cout<<“\nsum=“<<c;
getch();
}
Output:
a=20
b=10
sum=30
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
a=20;
b=10;
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
c=a+b;
cout<<“\nsum=“<<c;
getch();
}
Output:
a=20
b=10
sum=30
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a=20,b=10,c;
clrscr();
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
c=a+b;
cout<<“\nsum=“<<c;
getch();
}
Output:
a=20
b=10
sum=30
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a=20,b=10;
clrscr();
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
cout<<“\nsum=“<<a+b;
getch();
}
Output:
a=20
b=10
sum=30
|
Note: no.of lines reduced
|
Note:no.of lines reduced
|
Note:no.of lines reduced and Memory reduced
|
|
Program-5
|
Program-6
|
||
#include<iostream.h>
#include<conio.h>
void main()
{
int a=20,b=10;
clrscr();
cout<<“\na=”<<a<<”
\nb=“<<b;
cout<<”
\nsum=“<<a+b;
getch();
}
Output:
a=20
b=10
sum=30
|
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<“\na=”<<20<<”
\nb=“<<10;
cout<<”\nsum=“<<(20+10);
getch();
}
Output:
a=20
b=10
sum=30
|
||
Note: no.of lines reduced
|
Note: no.of lines reduced
|
//Write a Program of
demonstrate Arithmetical Operators
Program-1
|
Program-2
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
int b;
int c;
clrscr();
a=20;
b=10;
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
c=a+b;
cout<<“\nsum=“<<c;
c=a-b;
cout<<“\nsub=“<<c;
c=a*b;
cout<<“\nprod=“<<c;
c=a/b;
cout<<“\ndiv=“<<c;
c=a%b;
cout<<“\nrem=“<<c;
getch();
}
Output:
a=20
b=10
sum=30
sub=10
prod=200
div=2
rem=0
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
int b;
int c;
clrscr();
a=20;
b=10;
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
cout<<“\nsum=“<<a+b;
cout<<“\nsub=“<<
a-b;
cout<<“\nprod=“<<
a*b;
cout<<“\ndiv=“<<
a/b;
cout<<“\nrem=“<<
a%b;
getch();
}
Output:
a=20
b=10
sum=30
sub=10
prod=200
div=2
rem=0
|
//Write a Program finds area
and perimeter of square.
S
|
||
S
|
S
|
|
S
|
Program-
|
#include<iostream.h>
#include<conio.h>
void main()
{
float s=2.3,area,peri;
clrscr();
cout<<“\nside
of the square=“<<s;
area=s*s;
peri=4*s;
cout<<“\narea
of the square=“<<area;
cout<<“\nperi
of the square=“<<peri;
getch();
}
Output:
side of the
square=2.300000
area of the
square=5.290000
peri of the
square=9.200000
|
Note:
Set
precision(int): it is predefined function and it is used to set decimal places.
Source
code is available in iomanip.h
Ex:
Program
|
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
float s=2.3,area,peri;
clrscr();
cout<<“\nside
of the square=“<<setprecision(2)<<s;
area=s*s;
peri=4*s;
cout<<“\narea
of the square=“<<setprecision(2)<<area;
cout<<“\nperi
of the square=“<<setprecision(2)<<peri;
getch();
}
Output:
side of the
square=2.30
area of the
square=5.29
peri of the
square=9.20
|
cin:
It is used to accept the
data by the user at run time.
The source code is available
in iostream.h
Syntax:
cin>>var1>>var2….;
Ex: //Write a Program of sum of two Numbers
and accept the a,b values by the user at runtime.
Program-1
|
Program-2
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<“enter
the value of a:”;
cin>>a;
cout<<“enter
the value of b:”;
cin>>b;
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
cout<<“\nsum=“<<a+b;
getch();
}
Result:
Input:
Enter the value of
a:25
Enter the value of
b:5
Output:
a=25
b=5
sum=30
|
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<“enter
the value of a,b:”;
cin>>a>>b;
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
cout<<“\nsum=“<<a+b;
getch();
}
Result:
Input:
Enter the value of
a,b:25 5
Output:
a=25
b=5
sum=30
|
/*Write a Program finds
area and perimeter of square and accepts side of the square by the user at run
time.*/
area=s*s;
S
|
||
S
|
S
|
|
S
|
perimeter=4*s
Program-
|
#include<iostream.h>
#include<conio.h>
void
main()
{
float
s=2.3,area,peri;
clrscr();
cout<<“enter side of the
square:";
cin>>s;
cout<<“\nside of the
square=“<<setprecision(2)<<s;
area=s*s;
peri=4*s;
cout<<“\narea of the
square=“<<setprecision(2)<<area;
cout<<“\nperi of the
square=“<<setprecision(2)<<peri;
getch();
}
Result:
Input:
Enter side of the square:2.5
Output:
side of the square=2.50
area of the square=6.25
peri of the square=10.00
|
2. Comma operator:
it
is used to separate the expression or
statements.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
Inta,b,c;
clrscr(),
a=35,
b=1,
cout<<“\na=“<<a,
cout<<“\nb=“<<b,
c=a+b,
cout<<“\nsum=“<<c,
getch( );
}
Output:
a=35
b=1
sum=36
|
3. Relational Operators:
< ,>,
<=,>=,==,!= are called Relation operators.
The expression is true in
between two variables then it can be display 1 otherwise zero.
//Write a Program of
demonstrate Relational Operators
Program-1
|
Program-2
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=96,b=5,c;
clrscr( );
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
c=a<b;
cout<<“\n
a<b =“<<c;
c=a>b;
cout<<“\n
a>b =“<<c;
c=a<=b;
cout<<“\n
a<=b =“<<c;
c=a>=b;
cout<<“\n a>=b =“<<c;
c=a==b;
cout<<“\nr
a==b =“<<c;
c=a!=b;
cout<<“\n
a!=b =“<<c;
getch( );
}
Output:
a=96
b=5
a<b=0
a>b=1
a<=b=0
a>=b=1
a==b=0
a!=b=1
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=96,b=5,c;
clrscr( );
cout<<“\na=“<<a;
cout<<“\nb=“<<b;
cout<<”\n”<<a<<”<”<<b<<”=”<<(a<b);
cout<<”\n”<<a<<”>”<<b<<”=”<<(a>b);
cout<<”\n”<<a<<”<=”<<b<<”=”<<(a<=b);
cout<<”\n”<<a<<”>=”<<b<<”=”<<(a>=b);
cout<<”\n”<<a<<”==”<<b<<”=”<<(a==b);
cout<<”\n”<<a<<”!=”<<b<<”=”<<(a!=b);
getch( );
}
Output:
a=96
b=5
95<5=0
95>5=1
95<=5=0
95>=5=1
95==5=0
95!=5=1
|
4. Assignment Operator
We can assign a value for
the variable.
Ex: a=25, x=’t’ …etc.
5. Conditional Operators:
Syntax:
Condition? exp1:exp2;
Ex:56>32?cout<<“true”):cout<<“false”;
//write a program to demonstrate Conditional Operators.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr( );
56>32?cout<<“true”:cout<<“false”;
getch( );
}
Output:
True
//write a program to find greatest numbers of two numbers using
conditional operator.
#include<iostream.h>
#include<conio.h>
void main()
{
Inta,b,max;
clrscr( );
cout<<“enter the value
of a,b:”;
cin>>a>>b;
max=a>b?a:b;
cout<<“\max=“<<max;
getch( );
}
Result:
Input:
enter the value of a,b:96 45
Output:
Max=96
6. Logical operators
&& , || , ! are
called Logical operators
Logical AND(&&)
Truth table:
Condition 1
|
Condition 2
|
Output
|
T(1)
|
T(1)
|
T(1)
|
T(1)
|
F(0)
|
F(0)
|
F(0)
|
T(1)
|
F(0)
|
F(0)
|
F(0)
|
F(0)
|
By the above table we
understand condition1 &2 are true then output is true remaining cases are
false.
Logical OR(||)
Truth table:
Condition 1
|
Condition 2
|
Output
|
T(1)
|
T(1)
|
T(1)
|
T(1)
|
F(0)
|
T(1)
|
F(0)
|
T(1)
|
T(1)
|
F(0)
|
F(0)
|
F(0)
|
By the above table we
understand condition1 &2 are false then output is false remaining cases are
true.
Logical NOT(!)
Truth table:
Condition 1
|
Output
|
T(1)
|
F(0)
|
F(0)
|
T(1)
|
If the condition is true
then output is false.
If the condition is false
then output is true.
//write a program
demonstrates Logical operators.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
clrscr( );
cout<<“\n\n
Logical AND:”;
cout<<“\n1&&1
=“<<(1&&1);
cout<<“\n1&&0
=“<<(1&&0);
cout<<“\n0&&1
=“<<(0&&1);
cout<<“\n0&&0=“<<(0&&0);
cout<<“\n\n
Logical OR:”;
cout<<“\n1||1
=“<<(1||1);
cout<<“\n1||0
=“<<(1||0);
cout<<“\n0||1
=“<<(0||1);
cout<<“\n0||0=“<<(0||0);
cout<<“\n\n
Logical NOT:”;
cout<<“\n!1
=“<<(!1);
cout<<“\n!0=“<<(!0);
getch( );
}
Output:
Logical
AND:
1&&1=1
1&&0=0
0&&1=0
0&&0=0
Logical
OR:
1||1=1
1||0=1
0||1=1
0||0=0
Logical
NOT:
!1=0
!0=1
|
7.Bitwise operators:
& ,|,^,<<,>>
are called bitwise operators.
Bitwise AND(&)
Truth table:
Condition 1
|
Condition 2
|
Output
|
Ex:10&2:
10-
0000 1010
02- 0000 0010
10
& 2 - 0000 0010
=1*21+0*20
=2
|
1
|
1
|
1
|
|
1
|
0
|
0
|
|
0
|
1
|
0
|
|
0
|
0
|
0
|
By the above table we
understand condition1 &2 are true then output is true remaining cases are
false.
Bitwise OR(|)
Truth table:
Condition 1
|
Condition 2
|
Output
|
Ex:10|2:
10-
0000 1010
02- 0000 0010
10 |
2 - 0000 1010
=1*23+0*22+1*21+0*20
=10
|
1
|
1
|
1
|
|
1
|
0
|
1
|
|
0
|
1
|
1
|
|
0
|
0
|
0
|
By the above table we
understand condition1 &2 are false then output is false remaining cases are
true.
Bitwise XOR(^)
Truth table:
Condition 1
|
Condition 2
|
Output
|
Ex:10^2:
10-
0000 1010
02- 0000 0010
10 ^
2 - 0000 1000
=1*23+0*22+0*21+0*20
=8
|
1
|
1
|
0
|
|
1
|
0
|
1
|
|
0
|
1
|
1
|
|
0
|
0
|
0
|
By the above table we
understand
condition1 &2 is true
then output is false
condition1 &2 is false
then output is false
Remaining cases are true.
Bitwise leftshift(<<):
The
bits can be shifted from right to left
Ex:10<<2
=1*25+0*24+1*23+0*22+0*21+0*20
=40
Bitwise Right shift(>>):
The
bits can be shifted from left to right
Ex:10>>2
=1*21+0*20
=2
//write a program
demonstrates Bitwise operators.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
clrscr( );
cout<<“\n10&2=“<<(10&2);
cout<<“\n10|2=“<<(10|2);
cout<<“\n10^2=“<<(10^2);
cout<<“\n10<<2=“<<(10<<2);
cout<<“\n10>>2=“<<(10>>2);
getch( );
}
Output:
10&2=2
10|2=10
10^2=8
10<<2=40
10>>2=2
|
8. Increment and
Decrement operators:
Increment operators:
++ is called an increment
operator and the value increased by 1.
C++ is supported following
types of increment operators
Pre increment operator
Post increment operator
Pre increment operator:
++ is placed before the
variable is called pre increment operator.
Ex:++a,++b…etc.
//write a program
demonstrates pre increment operators.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=15;
clrscr( );
cout<<“\nBefore
increment a=“<<a;
++a;
cout<<“\nAfter
increment a=“<<a;
getch( );
}
Output:
Before
increment a=15
After
increment a=16
|
Post increment operator:
++ is placed after the
variable is called post increment operator.
Ex:a++,b++…etc.
//write a program
demonstrates post increment operators.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=15;
clrscr( );
cout<<“\nBefore
increment a=“<<a;
a++;
cout<<“\nAfter
increment a=“<<a;
getch( );
}
Output:
Before
increment a=15
After
increment a=16
|
Difference pre and post
increment operators.
In pre increment operators
the value can be increased first after that it display, when increasing and
display with in the single statement.
In post increment operators
the value can be displayed first after that it is increased, when increasing
and display with in the single statement.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=15;
clrscr( );
cout<<“\n
a=“<<a;
cout<<“\n
++a=“<<(++a);
cout<<“\n
a++=“<<(a++);
cout<<“\n
++a=“<<(++a);
cout<<“\n
a++=“<<(a++);
cout<<“\n
++a=“<<(++a);
cout<<“\n
a=“<<a;
getch( );
}
Output:
a=15
++a=16
a++=16
++a=18
a++=18
++a=20
a=20
|
Decrement operators:
- -is called a decrement
operator and it is decreased by 1.
C++ is supported following
types of decrement operators
Pre decrement operator
Post decrement operator
Pre decrement operator:
-- is placed before the
variable is called pre decrement operator.
Ex:- -a,- -b…etc.
//write a program
demonstrates pre decrement operators.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=15;
clrscr( );
cout<<“\nBefore
decrement a=“<<a;
-
- a;
cout<<“\nAfter
decrement a=“<<a;
getch( );
}
Output:
Before
decrement a=15
After
decrement a=14
|
Post decrement operator:
- - is placed after the
variable is called post decrement operator.
Ex:a- -,b- - …etc.
//write a program
demonstrates post decrement operators.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=15;
clrscr( );
cout<<“\nBefore
decrement a=“<<a;
a
- -;
cout<<“\nAfter
increment a=“<<a;
getch( );
}
Output:
Before
increment a=15
After
increment a=14
|
Difference pre and post
decrement operators.
In pre decrement operators
the value can be decreased first after that it is display, when decreasing and
display with in the single statement.
In post decrement operators
the value can be displayed first after that it is decreased, when decreasing
and display with in the single statement.
Program
|
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a=15;
clrscr( );
cout<<“\n
a=“<<a;
cout<<“\n
- -a=“<<(- -a);
cout<<“\n
a- -=“<<(a- -);
cout<<“\n
- -a=“<<(- -a);
cout<<“\n
a- -=“<<(a- -);
cout<<“\n
- -a=“<<(- -a);
cout<<“\n
a=“<<a;
getch( );
}
Output:
a=15
-
-a=14
a-
-=14
-
-a=12
a-
-=12
-
-a=10
a=10
|
9. Unary operators.
The operator is preceding
with one operand is called Unary operators.
Ex: ++a ,a++, - -a, a -
-,-a..etc
10. Binary operators.
The operator is preceding
with two operands is called Binary operators.
Ex: a+b, a*b,
a&&b…etc
No comments:
Post a Comment