Hello world Program in C++

In this article, Hello world Program in C++, we are going to learn our first C++ program which is printing “Hello World” on the console. We will learn to print “Hello World” as a fixed string and also as a dynamic input string from the user. So let us begin with our first Hello World program in C++.

1. Program to Print “Hello World” in C++


To print Hello, World!in C++ programming, we just need standard in-out operation code from C++ built-in library. This is present in the std namespace. So first we will include iostream.h header in our program and then we need to mention using namespace std. This will include all functionality of the std namespace in our program.

We will learn in later programs what are namespaces in C++.

#include<iostream>

using namespace std;

int main()

{

    std::cout << "Hello, World!";

    std::cout << endl;

    return 0;

}

 

Output

Hello, World!

2. Program to Print Mutiple Hello world in C++


Similarly, if we want to write the Hello World text on the console multiple times then we can make use of a loop. C++ has many loops supported that we will learn in our upcoming tutorials. Here is a short example to print multiple times.

#include<iostream>

using namespace std;

int main()

{

    for (int i = 0; i < 5; i++)

        std::cout << "Hello, World!\n";

    std::cout << endl;

 

    return 0;

}

Output

 Hello, World!

 Hello, World!

 Hello, World!

 Hello, World!

 Hello, World!

3. Program to Print ‘Hello User’ input by the user


In this example, we are taking Partial input from the user for “Hello World” by asking users to input their names. In this program, we will print an answer to “Hello World” as “Hello UserName”.

#include<iostream>

using namespace std;

int main()

{

    string username;

    std::cout << "Hello, World!\n";

    std::cout << endl;

    std::cout << "What is your Name sir: ";

    std::cin >> username;

    std::cout << "Hello " << username;

 

    return 0;

}

Output

Hello, World!

What is your Name sir: Sachin

Hello Sachin

4. Hello World Program using function


In this example, we have defined a function void fun_display() to print a hello world on the console. We are calling this function in main() to print our hello world.

Using namespace std;
#include<iostream>

void fun_display()
    {
      cout << "Hello World";
    }

int main()
{
    // Calling function 
   display(); 

    return 0;
}

Output

Hello World