C++ Program to print Hello World

In this program, we will print the hello world on the output screen. We will use cout object of class ostream and also have used // symbol to use comments in C++ to tell what that particular statement does.

We will print it in two different ways so that we can learn more about the cout object.



Print hello world

On separate lines

Using endl stream manipulator

We can achieve this by using the cout object. We will use endl. It is used to break the line and jump the cursor to the next line.

Code:

#include<iostream>

using namespace std;

int main(){
    // prints hello world on separate lines
    cout << "hello" << endl << "world";
    return 0;
}

Using the '\n' character

There is one more way of doing this. We can just include the newline(\n) character in between.

#include<iostream>

using namespace std;

int main(){
    // prints hello world on separate lines
    cout << "hello\nworld";
    return 0;
}

Both of the above-discussed methods will give the same output.

Output:-

hello
world

On the same line




We can achieve this by printing one full string.

There is one more way, in which we will use the << operator to concatenate the two strings.

Code:

#include<iostream>

using namespace std;

int main(){  
    // prints hello world on the same line
    cout << "hello world";    
    cout << endl;
    cout << "hello " << "world";  
    return 0;
}

 

Output:-

hello world
hello world

You can learn about many other C++ Programs Here.







The following two tabs change content below.
Amit Rawat

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...