Input and Output in C++

The cout and cin is a used for input and output in C++ language. Both are inbuilt library functions,defined in iostream.h (header file).

cout

  • The cout is a used for output. It prints the given statement to the console.

The syntax of cout is given below:

  • cout<<" message ";
  • The cout is used in conjunction with stream insertion operator (<<) to display the output on a console.

One example use cout

#include< iostream.h >
void main( )
{
cout<<"welcome to Tutorial Zone";
}

Output:

welcome to Tutorial Zone

cin

  • The cin It reads the input data from the console.

The syntax of cin is given below:

  • cin>>variable_name;
  • The cin is used in conjunction with stream extraction operator (>>) to read the input from a console.

One example use cin

#include< iostream.h >
void main( )
{
int num;
cout<<"Enter a number:";
cin>>num;
}

Output:

Enter a number:10

Let's see a simple example of input and output in C++ language that prints addition of two numbers.

#include< iostream.h >
void main()
{
int a,b,sum;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
sum=a+b;
cout<< endl << sum of two number is :"<< sum;
}

Output:

Enter first number:10
Enter second number:10
sum of two number is:20