3 Operators C++
3 Operators C++
Experiment 3
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
-- Decrement Operator
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 operand is less than or equal to right operand
>= Checks if the left operand is greater than or equal to right operand
Logical Operators:
These
an operators are used to perform logical operation on operands. These operators are given in the
following table as;
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
#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
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
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