Chapter 9 - Input and Output Operators
Chapter 9 - Input and Output Operators
Chapter 9 - Input and Output Operators
Chapter-9
INPUT AND OUTPUT OPERATORS
Introduction
The input output operations are done using library functions cin and cout objects of the class
iostream.
Using the standard input and output library, we will able to interact with the user by printing
message on the screen and getting the users input from the keyboard.
A stream is an object where a program can either insert/extract characters to/from it.
The standard C++ library includes the header file iostream, where the standard input and output
stream objects are declared.
Example:
cout<<Let us learn C++; // prints Let us learn C++ on the screen.
The << operator inserts the data that follows it into the stream preceding it.
The sentence in the instruction is enclosed between double quotes ( ), because it is constant string
of characters.
Whenever we want to use constant strings of characters we must enclose them between double
quotes () so that they can be clearly distinguished from the variables name.
Example:
cout<<sum; //prints sum
cout<<sum; //prints the content of the variable sum
In order to perform a line break on the output we must explicitly insert a new-line character into
cout.
In C++ a new-line character can be specified as \n (backslash n), the new-line character is an
escape sequence character and helps in formatting the output statement.
Program: To demonstrate the cout statement:
#include<iostream.h>
#include<conio.h>
void main( )
{
cout<<C++ is an Obejct Orinted Programming Language<<\n;
cout<<C++ is an case sensitive language\n;
getch();
}
2|Page
Chapter 9- Input and Output Operators I PUC, MDRPUC, Hassan
Similarly, when we want to output more than one result then this can be done using a single cout
statement with multiple stream insertion operators.
This is called cascading of input output operators.
Example:
cout<<Enter the first number;
cin>>a;
cout<<Enter the second number;
cin>>b;
Instead of using cin statement twice, we can use a single cin statement and input the two numbers
using multiple stream extraction operators.
cout<<Enter the two number;
cin>>a>>b;
Similarly, we can even output multiple results in a single cout statements using cascading of
stream insertion operators.
cout<<The sum of two number is<<sum<<endl;
2. The setw( ) Manipulator : The setw( ) manipulator sets the width of the field assign for the
output. It takes the size of the field (in number of character) as a parameter. The output will be
right justified. Example the code :
cout<<setw(6)<<R ;
Generates the following output on the screen (each underscore represent a blank space)
_____R
In order to use this manipulator, it is must to include header file iomanip.h
3|Page
Chapter 9- Input and Output Operators I PUC, MDRPUC, Hassan
Program 1: To fins the sum of two numbers: Program: Convert the temperature in
#include<iostream.h> Fahrenheit into Celsius:
#include<conio.h> #include<iostream.h>
void main( ) #include<conio.h>
{ void main( )
int a, b, add; {
clrscr( ); float fah, cel;
cout<<Enter the two numbers; clrscr( );
cin>>a>>b; cout<<Enter the value of Fahrenheit;
add = a + b; cin>>fah;
cout<<The sum of two number cel = ((5.0/9.0) * fah 32.0);
is<<sum<<endl; cout<<fah<<F = <<cel<< C<<endl;
getch(); getch();
} }
Program: To interchange the values of two Program: To interchange the values of two
variables using third variable. variables without using third variable.
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
int a, b, temp; int a, b;
clrscr( ); clrscr( );
cout<<Enter the two numbers; cout<<Enter the two numbers;
cin>>a>>b; cin>>a>>b;
cout<<Before Interchanging : a = cout<<Before Interchanging : a =
<<a<< and b = <<b<<endl; <<a<< and b = <<b<<endl;
temp = a; a=a+b;
a = b; b=ab;
b = temp; a=ab;
cout<<After Interchanging : a = cout<<After Interchanging : a =
<<a<< and b = <<b<<endl; <<a<< and b = <<b<<endl;
getch(); getch();
} }
4|Page
Chapter 9- Input and Output Operators I PUC, MDRPUC, Hassan
Program: To find the area and circumference Program: To find the area of triangle given
of a circle. three sides.
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main( ) #include<math.h>
{ void main( )
float rad, area, circum; {
clrscr( ); float s1, s2, s3, s, area;
cout<<Enter the radius; clrscr( );
cin>>rad; cout<<Enter the length of three sides;
area = 3.142 * rad * rad; cin>>s1>>s2>>s3;
circum = 2 * 3.142 * rad; s = (s1 + s2+ s3)/2;
cout<<Area of circle = <<area<<endl; area = sqrt( s* (s-s1) * (s-s2) * (s-
cout<<Circumference of circle = s3));
<<circum<<endl; cout<<Area of triangle = <<area<<endl;
getch(); getch();
} }
Program: To convert days into years, months Program: To convert seconds into hours,
and days. minutes and seconds.
#include<iostream.h> #include<iostream.h>
#include<conio.h> #include<conio.h>
void main( ) void main( )
{ {
int totaldays, days, year, month; int totalsec, min, hrs, secs;
clrscr( ); clrscr( );
cout<<Enter the total days; cout<<Enter the total seconds;
cin>>totaldays; cin>>totalsec;
year = totaldays/365; hrs = totalsec / 3600;
totaldays = totaldays % 365; totalsec = totalsec % 3600;
month = totaldays/30; min = totalsec/60;
days = totaldays % 30; secs = totalsec % 60;
cout<<Years = << years<<endl; cout<<Hours = <<hrs<<endl;
cout<<Months = <<month<<endl; cout<<Minutes = <<min<<endl;
cout<<Days = <<days<<endl; cout<<Seconds = << secs<<endl;
getch(); getch();
} }
**************
5|Page