Website For Developers

Output in C++



• The process of getting something from system is called output.
• The result of the program is the output of that program.
• For output in C++, we use cout stream object.
• cout stands for Console Output in C++.
• cout use to display standard output on screen.
• For standard input/output (cin/cout), we use header file iostream.h
• iostream.h contains the declaration of all standard input and output stream objects in C++.
• cout object is used with insertion << operator.


Syntax

cout << " Your Output Result ";


Output in C++ Example

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  cout<< "Sky is blue.. ";
  cout<< 123;
  getch();
  return 0;
}

Output

Sky is blue.. 123

Note: The string is always written with in double quotes and numeric constant is always written without double quotes. If numeric value written with-in double quotes then compiler considered it as a string.


Example of output in C++ without using namespace

#include<iostream>
#include<conio.h>
int main() {
  std::cout<< "Sky is blue.. ";
  std::cout<< 123;
  getch();
  return 0;
}

Output

Sky is blue.. 123

Note: If you not using namespace then you have to use std:: with cout and with every outside objects, variables, and function that you call in your program.


Write a program to display your name and your father name.

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  cout<< "My name is John. \nMy fathers name is Albert. ";
  getch();
  return 0;
}

Output

My name is John.
My fathers name is Albert.

Note: \n is a escape sequence used to insert new line. See the list of Escape sequence in C++


Write a program to display
*
**
***
****
*****

#include<iostream>
#include<conio.h>
using namespace std;
int main() {
  cout<< "*"<< endl<< "**"<< endl<< "***"<< endl<< "****"<< endl<< "*****";
  getch();
  return 0;
}

Output

*
**
***
****
*****