CP Lab 05
CP Lab 05
COMPUTER
PROGRAMMING
Experiment 5
Operators in C++
1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Purpose:
This experiment provides an introduction of different C++ programs using operators and
applying data type conversions”. Students will run and check more advanced programs
Objectives:
1) Know the different operators and data type conversion procedures available in C++:
2) run and check different but related programs consisting of different operators and data
from one data type to another using the right method according to the required logic.
5) Code::Blocks IDE16.01
2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
C++ Operators
Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while - is an operator used for subtraction.
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators
a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various other
arithmetic operators in C++.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
#include <iostream>
using namespace std;
int main() {
int a, b;
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
a = 7;
b = 2;
return 0;
}
Example
#include<iostream>
using namespace std;
main()
{
int var1=5,var2=3;
int var3=5.5, var4=3;
int var5=5, var6=6;
cout<<var1%var2<<endl;
cout<<var3%var4<<endl;
cout<<var5%var6<<endl;
return 0;
}
4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
C++ also provides increment and decrement operators: ++ and -- respectively. ++ increases the
value of the operand by 1, while -- decreases it by 1.
For example,
int num = 5;
// increasing num by 1
++num;
Here, the value of num gets increased to 6 from its initial value of 5.
#include <iostream>
using namespace std;
int main(){
int num1 = 240;
int num2 = 40;
num1++; num2--;
cout<<"num1++ is: "<<num1<<endl;
cout<<"num2-- is: "<<num2;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 100, result_a, result_b;
5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 100, result_a, result_b;
return 0;
}
Example 4:
6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include<iostream>
using namespace std;
main()
{
int var1=5, var2=6;
return 0;
}
// assign 5 to a
a = 5;
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
// 2 is assigned to a
a = 2;
// 7 is assigned to b
b = 7;
return 0;
}
8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 3;
b = 5;
bool result;
return 0;
}
Output
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1
3 >= 5 is 0
3 <= 5 is 1
&&
Logical AND.
expression1 && expression 2
True only if all the operands are true.
||
Logical OR.
expression1 || expression 2
True if at least one of the operands is true.
!
Logical NOT.
!expression
True only if the operand is false.
In C++, logical operators are commonly used in decision making. To further understand the
logical operators, let's see the following examples,
Suppose,
a=5
b=8
Then,
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
#include <iostream>
using namespace std;
int main(){
bool b1 = true;
bool b2 = false;
cout<<"b1 && b2: "<<(b1&&b2)<<endl;
cout<<"b1 || b2: "<<(b1||b2)<<endl;
cout<<"!(b1 && b2): "<<!(b1&&b2);
return 0;
& Operator:
Every variable is a named memory location and every memory location have its address
defined which can be accessed using ampersand (&) operator which denotes an address
in memory. Consider the following example which will print the address of the variables
defined:
#include <iostream>
using namespace std;
int main () {
int var1;
char var2[10];
11
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
return 0;
}
Ternary Operator
This operator evaluates a boolean expression and assign the value based on the result.
Syntax:
If the expression results true then the first value before the colon (:) is assigned to the variable num1
else the second value is assigned to the num1.
#include <iostream>
using namespace std;
int main(){
int num1, num2; num1 = 99;
/* num1 is not equal to 10 that's why
* the second value after colon is assigned
* to the variable num2
*/
num2 = (num1 == 10) ? 100: 200;
cout<<"num2: "<<num2<<endl;
/* num1 is equal to 99 that's why
* the first value is assigned
* to the variable num2
*/
num2 = (num1 == 99) ? 100: 200;
cout<<"num2: "<<num2;
return 0;
}
12
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Boolean Operator
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
bool a = 321, b;
/*Declare two bool variables. Bool store two type of value true or false and this show Boolean
integer value 1 for true and 0 for false like here “bool a” is initialized with 321 value it store 1 in it
let see*/
cout << "Bool a Contains: " << a; // it shows 1
int c = true; // it store value 1 for true and 0 for false
c = a + a; // a value is 1 and 1 add to 1 gives 2
cout << "\n Integer c contain: " << c; //print c value
b = c + a;
/* it 2 + 1 it gives 1 because its type is bool so it shows only 0 and 1*/
cout << "\n Bool b contain: " <<b;
return 0;
}
Boolean type: The boolean type, known in C++ as bool, A variable of this type can have values true and false
Task 1:
Write a C++ program that Implement Ternary Operator by inputting and integer number
form user and checks if entered integer is an even number then display the message as
“it’s an even number” to user otherwise it will display “it’s an odd number” message on
screen.
Task 2:
13
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Write a C++ program that Implement distance formula on two double type points p1 and
p2 after in 3D pace getting input from user. Display the Distance Formula along with the
Final answer.
Hint: P1 = (x1,y1,z1) and P2 = (x2,y2,z2).
14