Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

LAB-03 :

setw manipulator, Input using cin, Arithmetic Operators and Expressions, Type conversion, Assignment
statement, Increment & decrement operators

Output Manipulators

Output manipulators are operators used with the insertion operators (<<) to control or
manipulate how data is displayed. Manipulators are the most common way to control
output formatting. They are used to make the output data look appealing and make
programs user friendly. All the output manipulators in C++ are defined under the
‘iomanip.h’ header file.

The ‘setw’ manipulator

The ‘setw ‘manipulator is used to set the field width for the next insertion operator. It
applies only to the next element inserted in the output. We can use either the left or the
right direction to justify the data appropriately in the field. Output is right justified by
default. The default field width is just wide enough to hold the expression. The ‘setw’
operator overcomes this problem by allowing a fixed field width. Let us look at the following program
to understand how ‘setw’ overcomes the problems posed by default field widths.

# include<iostream.h>
# include<iomanip.h>
int main(void)
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<" City "<<"\t No. of Schools";
cout<<"\n Durban \t"<<Durban;
cout<<"\n Moscow \t"<<Moscow;
cout<<"\n Sydney \t"<<Sydney;
cout<<"\n Paris \t"<<Paris;

return 0;
}
You will observe, it is hard to read and compare these numbers. It would be better if they are right justified. Let’s use
the ‘setw’ manipulator to eliminate the problem by specifying field widths to the respective columns.

# include<iostream.h>
# include<iomanip.h>
void main()
{
int Durban=4000, Moscow=5000, Sydney=10, Paris=1;
cout<<setw(10)<<" City "<<setw(30)<<" No. of Schoolss \n";
cout<<setw(10)<<" Durban"<<setw(20)<<Durban<<"\n";
cout<<setw(10)<<" Moscow"<<setw(20)<<Moscow<<"\n";
cout<<setw(10)<<" Sydney"<<setw(20)<<Sydney<<"\n";
cout<<setw(10)<<" Paris"<<setw(20)<<Paris<<"\n";

return 0;
}
The cin Statement
Just as we have the ‘cout’ statement to display data on the screen, the ‘cin’ statement is
used to extract data from the user or insert data into a variable. The statement is used with
the extraction operators (>>). It is also defined under the <iostream.h> header file and has
the following syntax:

Syntax: cin>>variable1>>variable2…………….;

You may observe that we have used the extraction operator repeatedly ‘>>’ in the syntax.
This is perfectly valid and is known as cascading of operators. For example,

cin>>variable1>>variable2;

Cascading the extraction operators allow us to input multiple variables using a


single ‘cin’ statement. This is the same as using the ‘cin’ statement twice.

cin>>variable1;
cin>>variable2;

To understand how the ‘cin’ statement works let us create a program which enters two
integers from the user and displays their sum on the screen.

# include <iostream.h>
void main()
{
int a,b;
cout<<" Enter the two integers";
cin>>a>>b;
cout<<" The sum of the two integers "<<a<<" and "<<b<<" is "<<a+b;

return 0;
}

Note: Both ‘cin’ and ‘cout’ refer to the basic input/output devices usually keyboards and
monitors.
Arithmetic operators in C++

+ Used for addition


- Used for subtraction
* Used for multiplication
/ Used for division
% This operator is called the remainder or the modulus operator. It is used to find the remainder
when the integer value/variable is divided by another value/variable. This operator cannot be
used with floating type variables.

The piece of code given below illustrates use of arithmetic operators:

#include <iostream.h>

int main(void)
{
int a = 10, b=3;
cout << ” Sum = ” << a + b << endl;
cout << ” Difference = “<< a – b << endl;
cout << ” Product = “<<a * b <<endl;
cout << ”Quotient = “<<a /b << endl;
cout << ” Remainder = “<< a %b << endl;
return 0;
}

#include <iostream.h>
int main(void)
{
float salary;
cout<<"\aEnter your desired monthly salary:";
cout<<" Rs._______\b\b\b\b\b\b\b";
cin>>salary;
cout<<"\n\t"<<"Rs."<<salary<<" a month is Rs."<< salary * 12.0<<" a year.";
cout<<"\rGee!\n";

return 0;
}

Type Conversion
# include <iostream.h>
# include <conio.h>
int main(void)
{
clrscr();
int a=10;
float b=4.23;
double c;
c=a*b;
cout<<" c: "<<c;
}
C++ is very flexible when treating mixed datatypes. In C++, it is perfectly normal to perform any arithmetic operations
on variables of different data types. This is achieved by a process called ‘Automatic Type Conversion’. Such a
conversion takes place when two operands of different data types are encountered in the same expression. In such
a case the variable of lower data type is automatically converted to the variable with the higher data type. The following
Table gives the order of data types in C++ according to which type conversions take place.

Look at the following program to understand how type casting really works.

# include <iostream.h>
# include <conio.h>
int main(void)
{
clrscr();
int A=10;
float B=4.2;
float C=A/B;
cout<<"C:"<<C;

return 0;
}

So what happens when this program is executed. As we have already learnt that when the compiler confronts with
such mixed expressions in same statement it converts the variable belonging to the lower data type to the variable
belonging to the higher data type. That is in our program the variable ‘A’ is converted to a float variable and then
divided by the float variable ‘B’ so that the it can be assigned to the float variable ‘C.’ If suppose variable ‘C’ was of
type ‘double’ then both these variables ‘A’ and ‘B’ would get converted to type ‘double’.
The entire process is also explained in the following figure;

Assignment Statement/Operator:

The following table lists the assignment operators supported by the C language

Operator Description Example

= Simple assignment operator. Assigns values from right side C = A + B will assign the
operands to left side operand value of A + B to C

+= Add AND assignment operator. It adds the right operand to the left C += A is equivalent to C =
operand and assign the result to the left operand. C+A

-= Subtract AND assignment operator. It subtracts the right operand C -= A is equivalent to C = C


from the left operand and assigns the result to the left operand. -A

*= Multiply AND assignment operator. It multiplies the right operand C *= A is equivalent to C = C


with the left operand and assigns the result to the left operand. *A

/= Divide AND assignment operator. It divides the left operand with the C /= A is equivalent to C = C
right operand and assigns the result to the left operand. /A

%= Modulus AND assignment operator. It takes modulus using two C %= A is equivalent to C =


operands and assigns the result to the left operand. C%A

<<= Left shift AND assignment operator. C <<= 2 is same as C = C


<< 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C


>> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C &


2

^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2

|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2


Run the following program and observe the results

#include <iostream.h>
#include<conio.h>
int main(void)
{
int a = 21;
int c ;
clrscr();

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c = a;
cout<<" (c = a) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c += a;
cout<<"(c += a) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c -= a;
cout<<"(c -= a) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c *= a;
cout<<"(c *= a) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c /= a;
cout<<"(c /= a) Value of c = "<<c<<"\n";

c = 200;
cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;
c %= a;
cout<<"(c %= a) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c <<= 2;
cout<<"(c <<= 2) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c >>= 2;
cout<<"(c >>= 2) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c &= 2;
cout<<"(c &= 2) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c ^= 2;
cout<<"(c ^= 2) Value of c = "<<c<<"\n";

cout<<"Value of a ="<<a<<" and value of c="<<c<<" before operation"<<endl;


c |= 2;
cout<<"( c |= 2) Value of c = "<<c<<"\n";

return 0;
}
INCREMENT AND DECREMENT OPERATORS

 Increment operators are used to increase the value of the variable by one and decrement operators are used to
decrease the value of the variable by one in C programs.
 Syntax: Increment operator: ++var_name; (or) var_name++;
 Decrement operator: – -var_name; (or) var_name – -;

Example:
Increment operator : ++ i ; i ++ ;
Decrement operator: – – i ; i – – ;

POST and PRE INCREMENT or DECREMENT

Below table will explain the difference between pre/post increment and decrement operators in C programming
language.

Operator Operator/Description

Pre increment operator (++i) value of i is incremented before assigning it to the


variable i

Post increment operator value of i is incremented after assigning it to the variable


(i++) i

Pre decrement operator (--i) value of i is decremented before assigning it to the


variable i

Post decrement operator (i--) value of i is decremented after assigning it to variable i

EXAMPLE

#include <iostream.h>
#include<conio.h>

int main(void)
{

int a = 20;
int b=0;
clrscr();
b=a++;
cout<<"After b=a++ Value of b ="<<b<<" and value of a="<<a<<endl;
b=++a;
cout<<"After b=++a Value of b ="<<b<<" and value of a="<<a<<endl;
b=a--;
cout<<"After b=a-- Value of b ="<<b<<" and value of a="<<a<<endl;
b=--a;
cout<<"After b=--a Value of b ="<<b<<" and value of a="<<a<<endl;
return 0;
}

You might also like