how to cout in cpp

HOW TO COUT IN C++

In this article, you will learn how to cout in CPP  and use of cout in the program with the help of Examples. 


In the CPP language displaying things on the console is not a difficult task. First of all build-in library add on the top of the program. The library is used for cout and cin in the cpp language is iostream library.

The syntax of the cout in cpp:

cout<<" your message ";

The universal problem about to print hello world in c++.Here is an example of how to print hello world in cpp.

Print hello world


#include<iostream>
using namespace std;
int main()
{
cout<<" hello world"<<endl;
}

output:

hello world



how to include iostream library?

 #include<iostream> 

This library is used for displaying characters, integers, and float numbers. This library is also used for cin data like characters, integers, and float numbers.


Example :

#include<iostream>
using namespace std;
int main()
{
//first intialize the data for display primitive data types
int a=5;
char ch='t';
float ft=5.6;
//displaying messages on the screen
cout<<"display on the screen"<<endl;
//display data
cout<<a<<endl;//here  the printing of variable a
cout<<ch<<endl;//here the printing of variable  ch
cout<<ft<<endl;//here float variable print

return 0;
}

output
display on the screen
5
t
5.6

cout is also used to print patterns and shapes on the console. The best use of cout makes your's code output attractive and understandable to user. Here is the example of a print H letter.

How to print letter on the console in c++


#include<iostream>
using namespace std;
int main()
{
cout<<"HHHH"<<"    "<<"HHHH"<<endl;//here the print of first line
cout<<"HHHH"<<"    "<<"HHHH"<<endl;//this is 2nd line of console
cout<<"HHHH"<<"    "<<"HHHH"<<endl;//by using endl move to next line
cout<<"HHHH"<<"HHHH"<<"HHHH"<<endl;//center line of letter H
cout<<"HHHH"<<"HHHH"<<"HHHH"<<endl;//center line of H
cout<<"HHHH"<<"    "<<"HHHH"<<endl;//bottom part of letter H
cout<<"HHHH"<<"    "<<"HHHH"<<endl;//same done in this line
cout<<"HHHH"<<"    "<<"HHHH"<<endl;
return 0;
}

output:
cout in cpp

In this example, print the structure of H letter with the help of Hs letters. Keep in the mind where H letter print and where space print. In the first line, four H letters print, then four spaces, then again print four H letters. In the second and third lines, the same procedure repeated.


In the fourth line and fifth line,in the place of spaces H letters printed.
In the sixth, seventh, and eighth four H letters print, then four spaces, then again print four H letters.


 Print the triangle with the help of cout statement

#include<iostream>
using namespace std;
int main()
{
cout<<"     *"<<endl;//first line
cout<<"    * *"<<endl;//second line
cout<<"   *   *"<<endl;//third line
cout<<"  *     *"<<endl;//fourth line
cout<<" *       *"<<endl;//fifth line
cout<<"* * * * * *"<<endl;//sixth line

return 0;
}

Output:


In the above code, Print the triangle with the help of cout statement. By knowing where the space print and where character(asterisk) print. carefully use the spaces according to the shape of the triangle in every line display on the console.

The above examples will be helping you for understanding the function of cout in the cpp. After this having any kind of problem in displaying in cpp then comment below your problem.

Comments

Popular posts from this blog

CSS selector

C Program to convert binary to decimal number