Write a C++ program that displays the message "Hello, World!" on the console.
Input and Output:
This program does not require any input. The output is a straightforward message:
- Output: Hello, World!
Algorithm:
- Start the Program: Initialize the C++ program with necessary headers.
- Output Message: Display "Hello, World!" on the console.
- End the Program: Finish the execution.
Below is the C++ code for a simple "Hello, World!" program with comments.
// Include the library for input and output streams.
#include <iostream>
// Use the standard namespace to simplify code.
using namespace std;
int main() {
// Output the message "Hello, World!" to the console
cout << "Hello, World!" << endl;
// End the program and return 0 to indicate successful completion
return 0;
}
Key Points of the Program:
- Header File: The #include <iostream> directive includes the I/O stream library, allowing the use of cout.
- Namespace: using namespace std; allows us to use std library features like cout and endl without prefixing them with std::.
- Main Function: The main() function is the starting point for execution. It must return an integer, with 0 indicating success.
- Output Statement: cout << "Hello, World!" << endl; sends the "Hello, World!" string to the console output and moves the cursor to a new line (endl).
This program demonstrates the most basic structure of a C++ program and is perfect for beginners to compile and run to see immediate results.