Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Lab 08 C++ Operators

The document discusses different types of operators in C++ such as arithmetic, relational, logical, and increment/decrement operators as well as manipulators. It provides examples to demonstrate how each operator works and its purpose. The document concludes with three lab assignments involving using operators to calculate quadratic roots, determine if a number is even or odd, and demonstrate the setw manipulator.

Uploaded by

MUHAMMAD HAMZA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab 08 C++ Operators

The document discusses different types of operators in C++ such as arithmetic, relational, logical, and increment/decrement operators as well as manipulators. It provides examples to demonstrate how each operator works and its purpose. The document concludes with three lab assignments involving using operators to calculate quadratic roots, determine if a number is even or odd, and demonstrate the setw manipulator.

Uploaded by

MUHAMMAD HAMZA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment # 08

OBJECTIVE:

Familiarization and working with different types of operators in C++.

THEORY:

OPERATOR

Operators are the symbols that cause a program to do something on the variables.
Following are the operators which are commonly used in C language.

Arithmetic operators:
There are five arithmetic operators in C++. Those are:
Operator Purpose
+ addition.
- Subtraction.
* Multiplication.
/ Division.
% remainder after integer division.

The % operator some times to referred as modulus operator.

Increment and Decrement Operator:

C++ provides the unary increment operator (++), and the unary increment operator (--) for the
increment or decrement of one (1) to/from the variable. If increment or decrement operator is
placed before the variables, they are referred to as the pre increment or pre decrement operator,
respectively. If increment or decrement operators are placed after the variable, they are referred
as the post increment or post decrement operators
Example
#include<iostream>
main( )
{
int a=1;
cout<<++a; // increment first then display.
cout<<a++; // first display then increment.
cout<<a;
cout<< --a; // decrement first then display.
cout<<a--; // first display then decrement.
cout<<a;
}//Observe the output

Relational Operator:
There are four relational operators they are

Operators Meaning
< Less than.
<= Less than or equal to.
> Greater than.
>= Greater than or equal.

Closely associated with the relational operators are the following two equality operators.
Operators Meaning
== Equal to.
!= Not Equal to.

These six operators are used to form logical expression, which represents condition that is either
true or false. Resulting expression will be of type integer, since true is represented by integer
value 1 and false is represented by the value 0.

Example
#include<iostream>
main()
{
int a=1,b=2;
cout<<(a<b);
cout<<(a>b);
cout<<(++a = = b);
cout<<a<=b;
cout<<a>=b;
}

Observe the Output

Logical Operator:
C contains three logical operators, they are

Operators Meaning
&& And
|| OR
! Not

The logical operators act upon the operands that are themselves logical expressions. The net
effect is to combine the individual logical expressions into more complex conditions that are
either true or false. The result of logical and operation will be true only of all the expression are
true, where as the result of a logical or operator will be true if either expression will be true. In
other words, the result of logical or operation will be false when all expression will be false.

#include<iostream>
main()
{
int a=1,b=2;
cout<<( (a<b) && (a<=b));
cout<<( (a= =b) || (a<b));
cout<<( !(a= =b));
} //Observe the Output

Manipulators
Manipulators are operators used with the insertion operator to modify or manipulate the way in
which data is displayed. The function prototype for the manipulators is the iomanip.h header file.
Two of the most common manipulators are endl and setw().

endl is used for new line


setw() is used setting the specific width.

Example
#include<iostream>
#include<iomanip.h>
main()
{
cout<<”UCET”<<setw(5)<<”UNIVERSITY”<<endl<<”IUB”;
}
Observe how manipulators perform their function. Also search some other manipulators from the
help & right down the function of at least three by taking suitable examples

Output of the above program;


UCET UNIVERSITY
IUB

Lab Assignment

Q#01. Write a program that calculates the roots of Quadratic equation?

Where X1= (-b+(b*b-4*a*c) / (2*a) & X2= (-b -(b*b-4*a*c) / (2*a)

Q#02. Write a program that inputs a number and displays whether it is even or odd by using
logical operator.

Q#0. Write a program that explains the use of ‘setw’ manipulator?

You might also like