0% found this document useful (0 votes)
8 views7 pages

3 Operators C++

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

3 Operators C++

Uploaded by

Zulqurnan Anjum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Programming Lab

Experiment 3

Exploring Various Types of Operators in C++


Objectives:

 To understand operators in C++
 To use various operators in programs

Operators:

anoperator is a symbol to perform some specific functionality. The values/variables on which operator performs
An
specific function are called operands. Based on the number of operands, the operators in C++ are:
 Unary Operators
 Binary Operators
 Ternary Operators
Based on the functionality, the operators in C++ are:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Other/Miscellaneous Operators

Arithmetic Operators:

Arithmetic
an operators are used to perform some mathematical operation on given variables. Arithmetic operators used in C++
are:

Operator Function
+ Adds the operands

- Subtracts the operands

* Multiply the operands

/ Divide the operands


% Remainder after division
++ Increment operator

-- Decrement Operator

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 3
The following codes give idea about arithmetic operators:
#include<iostream>
#include<conio.h>
int main(){
int x=20;
int y=10;
int add;
int subtract;
int multiply;
int remainder;
int divide;
add=x+y;
subtract=x-y;
multiply=x*y;
divide=x/y;
remainder=x%y;
cout<<"The addition is"<<add<<endl;
cout<<"The subtraction is"<<subtract<<endl;
cout<<"The multiplication is"<<multiply<<endl;
cout<<"The division is"<<divide<<endl;
cout<<"The remainder is"<<remainder<<endl;
getch();
return 0;
}
For increment and decrement operators, the following are used;
 Post Increment
 Post Decrement
 Pre Increment
 Pre decrement
The following code gives the idea of these operators:
#include<iostream>
#include<conio.h>
int main(){
int x=20;
int y=10;
int z=30;
int k=40;
int j;
int l;
int m;
int n;
j=x++;
m=++y;
l=z--;
n=--k;
cout<<"The post increment of j is"<<j<<endl;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 3
cout<<"The pre increment of y is"<<m<<endl;
cout<<"The post decrement of l is"<<l<<endl;
cout<<"The pre decrement of k is"<<n<<endl;
getch();
return 0;
}

Relational Operators:

Relational operators give relation between operands. Relational operators used in C++ are:
an

Operator Function
< Checks if the left operand is less than right operand
> Checks if the right operand is less than left operand
== Checks if the left and right operands are equal

!= Checks if the left and right operands are not equal

<= Checks if the left operand is less than or equal to right operand

>= Checks if the left operand is greater than or equal to right operand

The following code gives idea about relational operators:


#include<iostream>
#include<conio.h>
int main(){
int x=20;
int y=10;
cout<<"x is less than y"<<(x<y)<<endl;
cout<<"x is greater than x"<<(y<x)<<endl;
cout<<"x is equal to y"<<(x==y)<<endl;
cout<<"x is not equal to y"<<(x!=y)<<endl;
cout<<"x is less than or equal to y"<<(x<=y)<<endl;
cout<<"x is greater than or equal to y"<<(x>=y)<<endl;
getch();
return 0;
}

Logical Operators:

These
an operators are used to perform logical operation on operands. These operators are given in the
following table as;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 3

Operator Function
&& Logical AND will give true only if both operands are true
|| Logical OR will give true if any one of the operands is true

! Logical NOT reverses the state

The following code gives idea about these operators:

#include<iostream>
#include<conio.h>
int main(){
int x=20,y=30,z;
cout<<"please enter the number";
cin>>z;
if((z>x)&&(z>y)){
cout<<"the entered number "<<z<<" is greater than 30"<<endl;
}
if((z>x)&&(z<y)){
cout<<"the entered number "<<z<<" is less than 30"<<endl;
}
if((z<x)&&(z<y)){
cout<<"the entered number "<<z<<" is greater than 20"<<endl;
}
if((z>x)||(z<y)){
cout<<"the entered number "<<z<<" is entered"<<endl;
}
if(!z==0){
cout<<"the entered number "<<z<<" has been removed"<<endl;
}
getch();
return 0;
}

Bitwise Operators:

an
These operators perform operation on bitwise basis. The following table gives the idea:

Operator Function
& Performs the bit wise and operation on operands

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 3

| Performs the or operation on bitwise basis on operands


^ Performs the bit wise exclusive or operation on operands

~ Performs the bit wise not operation on operands

<< Performs the bit wise left shift operation on operands


>> Performs the bit wise right shift operation on operands
The following
code gives the idea:
#include<iostream>
#include<conio.h>
int main(){
int x=26,y=9;
cout<<(x&y)<<endl;
cout<<(x|y)<<endl;
cout<<(x^y)<<endl;
cout<<(~x)<<endl;
cout<<(x<<1)<<endl;
cout<<(x>>1)<<endl;
getch();
return 0;
}

Assignment Operators:

Assignment
an operators assign the value and are given as: (Refer to the class lecture for further
understanding)

Operator Function
= Simple assignment operator

+= Add and assignment operator

%= Modulus and assignment operator

-= Subtract and assignment operator

*= Multiply and assignment operator

/= Divide and assignment operator

The following code gives the idea:


#include<iostream>
#include<conio.h>
int main(){
int x=26,y=9;

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah


Computer Programming Lab
Experiment 3

x+=1;
cout<<x<<endl;
x-=1;
cout<<x<<endl;
x/=2;
cout<<x<<endl;
x%=2;
cout<<x<<endl;
x*=2;
cout<<x<<endl;
getch();
return 0;
}

Miscellaneous Operators:

Some of the other operators used in C++ include sizeof, conditional operator, type cast, comma, pointer
an
operators etc. Some of them have been used here at this moment:

#include<iostream>
#include<conio.h>
int main(){
int x=10,y=20,z,n;
float m=8.9;
cout<<"plz enter the number"<<endl;
cin>>z;
cout<<sizeof(int)<<endl;
n=((z<x)&&(z<y))? z:y ;
cout<<int(m)<<endl;
cout<<n<<endl;
getch();
return 0;
}

Tasks:
1) Write a program that takes a number input from user and returns the double of that input.
2) Write a program that takes length and width of rectangle from user and outputs the area and
perimeter of rectangle.
3) Write a program that takes input of length in feet and outputs the length in inches.
4) Write a program that takes input of length in inches and returns the length in feet and inches.
5) Write a program that takes number input from user and outputs whether the number is even or odd.
6) Write a program that takes input from user and tells whether the number is between 20 and 30.
7) Write a program that takes floating point input from user outputs the integer part.
8) Write a program that takes integer input from user and outputs the result of assignment operators
Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah
Computer Programming Lab
Experiment 3

on the input.
9) Write a program that takes 3 inputs from user and outputs the result of binary operators on any of
two inputs.
10) Given the function body:
int x=10,y=14,z,l,m,n;
z=x++;
cout<<x<<endl<<z<<endl;
l=++x;
cout<<x<<endl<<l<<endl;
m=x--;
cout<<x<<endl<<m<<endl;
n=--x;
cout<<x<<endl<<n<<endl;
First analyze the ouput and then verify on the code.

Conclusion:

an

Designed & Prepared by Engr. Syed Ahtisham Mehmood Shah

You might also like