TOPIC: Insertion and Extraction Operators
In the second part of C++ Programming, we will discuss about the
Insertion and Extraction Operators. They are used with the
Input and the Output statements. These are the standard
Input/Output statements which are linked with 'namespace
std'.
They are written as '>> ' for inserting elements and
'
<< ' for
extracting the elements. We use 'cin>>' for
inserting and 'cout<< ' for extracting.
If we do not want to use
'namespace
std', then we
can use
'std:cin>>' for
inserting and
'std:cout<< ' for
extracting
elements.
But this
method is
not
recommended
as it
consumes
more time
and
it is not
a good
method to
follow.
Let us
take an
example
for adding
two
numbers
and
finding
their
sum. We
will
accept the
first
number
using the
'cin>>'
operator,
then we
will
accept the
second
number
again.
By doing
so, we
will be
able to
print the
output
using the
'cout<< '
operator.
NB:
→
'cin>>'
is
also
known
as
the
Input
statement
→
'cout<< ' is
also
known
as
the
Output
statement
Talk is cheap, Show me the code
- Linus Torvalds
C++ Program to add two inputs
Code written in Visual Studio Code
// Program to add two inputs
#include<iostream>
using namespace std;
int main()
{
int num1, num2;
cout<< "Enter the value of num1\n"; // Output prompt
cin>>num1; // Input prompt
cout<<"\nEnter the value of num2\n";
cin>>num2;
cout<<"\nThe sum is " << num1+num2;;
return 0;
}
Enter the value of num1
5
Enter the value of num2
9
The sum is 14