0% found this document useful (0 votes)
11 views14 pages

CP Lab 05

This document provides an introduction to operators in C++ programming. It discusses arithmetic, assignment, relational, increment/decrement and other types of operators. Examples are given to demonstrate how to use each operator type to perform operations on variables and check relationships. The objectives are to learn how to use different C++ operators and perform data type conversions in programs. Dev-C++ and Code::Blocks IDEs are listed as tools for developing and running programs.

Uploaded by

Hanzla Zafar
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)
11 views14 pages

CP Lab 05

This document provides an introduction to operators in C++ programming. It discusses arithmetic, assignment, relational, increment/decrement and other types of operators. Examples are given to demonstrate how to use each operator type to perform operations on variables and check relationships. The objectives are to learn how to use different C++ operators and perform data type conversions in programs. Dev-C++ and Code::Blocks IDEs are listed as tools for developing and running programs.

Uploaded by

Hanzla Zafar
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/ 14

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

COMPUTER
PROGRAMMING

Experiment 5
Operators in C++

CLO 2. Use modern tools and languages for solving problems of


varying complexities

CLO 3. Construct the experiments/projects of varying


complexities.

CLO 4. Demonstrate unique solution of problem under discussion.

1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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

consisting of different C++ operators and data type conversions.

Objectives:

At the end of this experiment you will:

1) Know the different operators and data type conversion procedures available in C++:

how to use them in a C++ program.

2) run and check different but related programs consisting of different operators and data

type conversion procedures

3) Implementation of C++ program consisting of different operators types and convert

from one data type to another using the right method according to the required logic.

Equipment and Components:

4) Dev-C++ 5.0 Beta 9.2 (4.9.9.2) 9.0 MB with Minge/GCC 3.4.2

5) Code::Blocks IDE16.01

2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

Operators in C++ can be classified into 6 types:

1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Bitwise Operators
6. Other Operators

C++ Arithmetic Operators


Arithmetic operators are used to perform arithmetic operations on variables and data. For
example,

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)

Example 1: Arithmetic Operators

#include <iostream>
using namespace std;

int main() {
int a, b;
3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

a = 7;
b = 2;

// printing the sum of a and b


cout << "a + b = " << (a + b) << endl;

// printing the difference of a and b


cout << "a - b = " << (a - b) << endl;

// printing the product of a and b


cout << "a * b = " << (a * b) << endl;

// printing the division of a by b


cout << "a / b = " << (a / b) << endl;

// printing the modulo of a by b


cout << "a % b = " << (a % b) << endl;

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

SOFTWARE ENGINEERING DEPARTMENT

Increment and Decrement Operators

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.

Auto-increment and Auto-decrement Operators

#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;
}

Example 2: Pre-Increment and Pre-Decrement Operators

// Working of pre-increment and pre-decrement operators

#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

SOFTWARE ENGINEERING DEPARTMENT

// incrementing a by 1 and storing the result in result_a


result_a = ++a;
cout << "result_a = " << result_a << endl;
cout << "a = " << a << endl;

// decrementing b by 1 and storing the result in result_b


result_b = --b;
cout << "result_b = " << result_b << endl;
cout << "b = " << b << endl;

return 0;
}

Example 3: Post Increment and Post Decrement Operators

// Working of post-increment and post-decrement operators

#include <iostream>
using namespace std;

int main() {
int a = 10, b = 100, result_a, result_b;

// incrementing a by 1 and storing the result in result_a


result_a = a++;
cout << "result_a = " << result_a << endl;

// decrementing b by 1 and storing the result in result_b


result_b = b--;
cout << "result_b = " << result_b << endl;

return 0;
}

Example 4:

6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

#include<iostream>
using namespace std;

main()
{
int var1=5, var2=6;

cout<<" Pre Increment ++ var1: "<< ++var1<<endl;


cout<<"Now in Var 1 : "<<var1;
cout<<endl<<endl;

cout<<" Post Increment var1++ : "<< var1++<<endl;


cout<<"Now in Var 1 : "<<var1;
cout<<endl<<endl;

cout<<"Pre decrement : "<<--var1<<endl;


cout<<"Now in Var 1 : "<<var1;
cout<<endl<<endl;

cout<<" Post decrement : "<<var1--<<endl;


cout<<"Now in Var 1 : "<<var1;
cout<<endl<<endl;

return 0;
}

2. C++ Assignment Operators


In C++, assignment operators are used to assign values to variables. For example,

// assign 5 to a
a = 5;

Here, we have assigned a value of 5 to the variable a.

Operator Example Equivalent to


= a = b; a = b;
7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Operator Example Equivalent to


+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;

Example 2: Assignment Operators

#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;

// value of a is assigned to temp


temp = a; // temp will be 2
cout << "temp = " << temp << endl;

// assigning the sum of a and b to a


a += b; // a = a +b
cout << "a = " << a << endl;

return 0;
}

3. C++ Relational Operators


A relational operator is used to check the relationship between two operands. For example,

// checks if a is greater than b


a > b;

8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Here, > is a relational operator. It checks if a is greater than b or not.

If the relation is true, it returns 1 whereas if the relation is false, it returns 0.

Operator Meaning Example


== Is Equal To 3 == 5 gives us false
!= Not Equal To 3 != 5 gives us true
> Greater Than 3 > 5 gives us false
< Less Than 3 < 5 gives us true
>= Greater Than or Equal To 3 >= 5 give us false
<= Less Than or Equal To 3 <= 5 gives us true

Example 4: Relational Operators

#include <iostream>
using namespace std;

int main() {
int a, b;
a = 3;
b = 5;
bool result;

result = (a == b); // false


cout << "3 == 5 is " << result << endl;

result = (a != b); // true


cout << "3 != 5 is " << result << endl;

result = a > b; // false


cout << "3 > 5 is " << result << endl;

result = a < b; // true


cout << "3 < 5 is " << result << endl;

result = a >= b; // false


cout << "3 >= 5 is " << result << endl;

result = a <= b; // true


cout << "3 <= 5 is " << result << endl;
9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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

Note: Relational operators are used in decision making and loops.

4. C++ Logical Operators


Logical operators are used to check whether an expression is true or false. If the expression is
true, it returns 1 whereas if the expression is false, it returns 0.

Operator Example Meaning

&&
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,

(a > 3) && (b > 5) evaluates to true

10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

(a > 3) && (b < 5) evaluates to false

(a > 3) || (b > 5) evaluates to true


(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false

!(a == 3) evaluates to true


!(a > 3) evaluates to false

Example 5: Logical Operators

#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];

cout << "Address of var1 variable: ";

11
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

cout << &var1 << endl;

cout << "Address of var2 variable: ";


cout << &var2 << endl;

return 0;
}

Ternary Operator
This operator evaluates a boolean expression and assign the value based on the result.
Syntax:

variable num1 = (expression) ? value if true : value if


false

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

SOFTWARE ENGINEERING DEPARTMENT

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

SOFTWARE ENGINEERING DEPARTMENT

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

You might also like