11th Computer Science 12th Computer Science BCA CBSE CBSE JAC JAC

Operators in C++ Programming Language

Operators in C++ Programming Language
Written by AIPS

Operators are the symbol that represent operation between operands.

C=A+B

In above example A,B and C are the operands and +, = are the operators.

Note:-

Operand:- on which operation is to be performed.

Expression:- combination of operator and operands.

There are following types of operators in C++ language that perform different operation.

  • Arithmetic operators +,-,*,/,%
  • Relation operators >,<, >=,<=,= =, !=
  • Logical operators && ,|| ,!
  • Assignment operator =
  • Increment / Decrement operator ++, – –
  • Bitwise operators &, |,^, ~, <<, >>
  • Special operators sizeof(), comma
  • Conditional or Ternary operator ? :

Note:-

There are three category of Operators in C++.

  1. Unary operator:- which Requires one operand to operate.

Ex.. increment (++) and Decrement (- -) etc.

2. Binary operator:- Which Requires two operands to operate.

Ex….. Arithmetic,Relational,Logical ,..etc

3. Ternary operator :- which requires three operands to operate.

ex…Ternary operator.

  1. Arithmetic operators: – The operators that are used for arithmetic calculations, known as Arithmetic operators.

Symbol……………………… Purpose

+……………….     Addition

-…………………..Subtraction

*…………………..Multiplication

/…………………..Division

%……………………….Modulo

2. Relational operators: – These operators are used for comparison. These are also called comparison operators.

Symbol……………………….Purpose

>……………….     Greater than

<…………………..Less than

>=…………………..Greater than or equal to

<=…………………..Less than or equal to

= =……………………….Equal to

!=………………………Not Equal to

3. Logical operators: – These operators are used to combine or negate conditions.

Symbol………………………Purpose

&&………………………….Logical AND

||……………………………Logical OR

!……………………………………… Logical NOT

4. Assignment operator:- This operator is used to Assign value to a variable.

Ex..  a=5;                

5. Increment or Decrement operator:- It is used to increase or decrease the value of a variable by one. It can be used in two ways.

  1. Pre-Increment \ Decrement:- when the operator is placed before the operands.

    Example   ++a, – -a;

  • 2.Post-Increment \ Decrement:- when the operator is placed after the operand.

Example   a++, a- –

  Q. Write a program to represent pre-increment.

#include<iostream.h>
#include<conio.h>
void main()  
{	
clrscr();	
int a,b;
a=5;
b=++a;	
cout<<”a=”<<a;
cout<<”\n b=”<<b;
getch();
}

Output

a=6

b=6

Q. Write a program to represent pre-decrement.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b;
a=5;
b= - -a;	
cout<<”a=”<<a;
cout<<”\n b=”<<b;
getch();
}

Output

a=4

b=4

Q. Write a program to represent post-increment.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b;
a=5;
b=a++;	
cout<<”a=”<<a;
cout<<”\n b=”<<b;
getch();
}

Output

a=6

b=5

Q. Write a program to represent post-decrement.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b;
a=5;
b=a- -;	
cout<<”a=”<<a;
cout<<”\n b=”<<b;
getch();
}

Output

a=4

b=5

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
++a;
a++;
++a;
a++;
cout<<”a=”<<a;
getch();
}

Output

a=9

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
--a;
a--;
--a;
a--;
cout<<”a=”<<a;
getch();
}

Output

a=1

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
++a;
a++;
++a;
a--;
--a;
cout<<”a=”<<a;
getch();
}

Output

a=6

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<++a;
getch();
}

Output

6

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<- -a;
getch();
}

Output

4

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<++a<<++a<<++a;
getch();
}

Output

8 7 6

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<- -a<<- -a<<- -a;
getch();
}

Output

2 3 4

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<a++<<a++<<a++;
getch();
}

Output

7 6 5

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<a- -<<a- -<<a- -;
getch();
}

Output

3 4 5

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<a++<<++a<<a++;
getch();
}

Output

7 7 5

Q. Find Output

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a;
a=5;
cout<<a- -<<- -a<<a- -;
getch();
}

Output

3 3 5

Q. Evaluate  the  following  C++ expression s where  a,b,c are integers and d,f are floating point numbers .The values are   a=5,b=6 and d=3.5.

   a)    c=a++ – (b ++) * (- -d)

   b)    f=(++b)  * b – a++

Q.  Evaluate  the  following  C++ expression s where  a,b,c are integers and d,f are floating point numbers .The values are   a=3,b=5 and d=1.5.

   a)    c=a++ +b*++d

   b)    f= b*b++ – ++a

Write the output of the following program:

#include<iostream.h>
void main()
{
int x=5,y=5;
cout<<x++;
cout<<”,”;
cout<<++x;
cout<<”,”;
cout<<y++<<”,”<<++y;
}

Q. Evaluate  the  following  C++ expression s where  a,b,c are integers and d,f are floating point numbers .The values are   a=4,b=4 and d=1.5.

   a)    c=a+++++b*++d

   b)    f=++ b*b++ – ++a

Q. For a given variable “a=, initialized by 10, what is the difference between statements  a++ and ++a? Explain briefly

Bitwise operators:- These are used for bit manipulation. it can be classified into two groups.

  1. Bitwise Logical
  2. Bitwise shift

&………………………..Bitwise AND

|…………………………Bitwise OR

~ ………………………..Bitwise NOT

^…………………………Bitwise XOR

<< ………………………Left shift

>>……………………….Right shift

Bitwise AND: – This operator returns 1 when both bits are 1 otherwise it returns 0.

A…………………B…………………….A&B

0…………………0……………………0

0………………….1…………………..0

1…………………..0…………………0

1…………………..1………………….1

Rule:-

  • Find binary of First variable.
  • Find binary of Second variable.
  • compare binary of both variable according to above rule.
  • Find decimal of what we get from comparison.

Q.Write a program to represent Bitwise AND.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b,c;
a=10;
b=5;
c=a&b;
cout<<c;
getch();
}

Output

0

Bitwise OR: – This operator returns 0 when both bits are 0 otherwise it returns 1.

A…………………B…………………….A|B

0…………………0……………………0

0………………….1…………………..1

1…………………..0…………………1

1…………………..1………………….1

Rule:-

  • Find binary of First variable.
  • Find binary of Second variable.
  • compare binary of both variable according to above rule.
  • Find decimal of what we get from comparison.

Q.Write a program to represent Bitwise OR.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b,c;
a=10;
b=5;
c=a|b;
cout<<c;
getch();
}

Output

15

Bitwise XOR: – This operator returns 0 when both bits are same otherwise it returns 1.

A…………………B…………………….A^B

0…………………0……………………0

0………………….1…………………..1

1…………………..0…………………1

1…………………..1………………….0

Rule:-

  • Find binary of First variable.
  • Find binary of Second variable.
  • compare binary of both variable according to above rule.
  • Find decimal of what we get from comparison.

Q. Write a program to represent Bitwise XOR.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b,c;
a=10;
b=5;
c=a^b;
cout<<c;
getch();
}

Output

15

Bitwise NOT:- It inverts 0 to 1 and vice versa

A………………….~A

0……………………..1

1………………………0

Rule:-

  • Find binary of variable.
  • change according to above rule.
  • Find decimal of what we get form change.

Q. Write a program to represent Bitwise NOT.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b;
a=5;
b=~a;
cout<<b;
getch();
}

Output

-6

Left Shift Rule

  • Find binary of given variable.
  • Add zero ‘how many’ bits to be shifted.
  • write decimal of all bits.

Q. Write a program to represent Left shift.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b;
a=5;
b=a<<2;
cout<<b;
getch();
}

Output

20

Right Shift Rule

  • Find binary of given variable.
  • Remove zero ‘how many’ bits to be shifted.
  • write decimal of Remaining bits.

Q. Write a program to represent Right shift.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int a,b;
a=5;
b=a>>2;
cout<<b;
getch();
}

Output

1

Special operators:- These operators are used for special purpose.

sizeof( )

This operator is used to find size of data type or variable.

Syntax

sizeof(datatype / Variable);

Q. Write a program to represent sizeof( ) operator.

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
cout<<sizeof(int);
getch();
}

Output

2

OR

#include<iostream.h>
#include<conio.h>
void main()  
{
clrscr();	
int z;
z= sizeof(int);
cout<<”size of integer=”<<z;
getch();
}

Output

size of integer= 2

Conditional or Ternary operator:-

It is a conditional statement and used for making decision when we have two options.

It requires three operands so , also known as ternary operator.

syntax

condition ? <true block> : <false block> ;

Comma(,)

This operator is used for variable separator.

Q. write a program to check person is eligible for vote or not. using Ternary or conditional operator.compile time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
a=10;
a>=18?cout<<” eligible for vote”:cout<<” not eligible for vote”;
getch();
}

output

not eligible for vote

Q. write a program to check person is eligible for vote or not. (using Ternary or conditional operator.Run time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<”Enter age”;
cin>>a;
a>=18?cout<<” eligible for vote”:cout<<” not eligible for vote”;
getch();
}

output

Enter age=12

not eligible for vote

Q. write a program to check number is even or odd. (using Ternary or conditional operator.compile time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
n=5;
n%2==0?cout<<” Even number”:cout<<”Odd number”;
getch();
}

output

Odd number

Q. write a program to check number is even or odd. (using Ternary or conditional operator.Run time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<”Enter any number=”;
cin>>n;
n%2==0?cout<<” Even number”:cout<<”Odd number”;
getch();
}

output

Enter any number=5

Odd number

Also Read:- C++ Programming Tutorial

Also Read:- computer Science Exam 2020

About the author

AIPS

Leave a Comment