# Print Hexagon Outline using C++

### Introduction

In this article, we will explore how to print outline of a hexagon shape using the C++ programming language. We will provide a complete code example, explain each step of the code, and demonstrate the output. So let’s dive in and learn how to print the outline of a hexagon using C++!

Before you proceed with it, you can also look at How To Print Hexagon using C++

### Code Example

``````#include <iostream>

int main() {
int size;
std::cout << "Enter the size of the hexagon: ";
std::cin >> size;

// Print upper triangle outline
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
std::cout << " ";
}
std::cout << "*";
for (int j = 0; j < 2 * i - 1; j++) {
std::cout << " ";
}
if (i != 0) {
std::cout << "*";
}
std::cout << std::endl;
}

// Print middle area
for (int i = 0; i < size ; i++) {
std::cout << "* ";
for (int j = 0; j < size - 2; j++) {
std::cout << "  ";
}
std::cout << "* " << std::endl;
}

// Print lower triangle outline
for (int i = size - 2; i >= 0; i--) {
for (int j = 0; j < size - i - 1; j++) {
std::cout << " ";
}
std::cout << "*";
for (int j = 0; j < 2 * i - 1; j++) {
std::cout << " ";
}
if (i != 0) {
std::cout << "*";
}
std::cout << std::endl;
}

return 0;
}``````

### Output

``````Enter the size of the hexagon: 6
*
* *
*   *
*     *
*       *
*         *
*         *
*         *
*         *
*       *
*     *
*   *
* *
* ``````

### Step-by-Step Explanation

1. The user is prompted to enter the size of the hexagon.
2. The first loop prints the outline of the upper triangle of the hexagon. It iterates from 0 to `size - 2`, representing each row of the upper triangle outline. In each iteration, it prints the required number of spaces to align the outline properly, followed by an asterisk (*), and then the required number of spaces to create the outline effect.
3. The second loop prints the middle area of the hexagon. It iterates `size` number of times to represent each row of the middle area. In each iteration, it prints an asterisk () followed by the required number of spaces to maintain the outline, and then another asterisk () to complete the row.
4. The third loop prints the outline of the lower triangle of the hexagon. It iterates from `size - 2` to 0, representing each row of the lower triangle outline. In each iteration, it prints the required number of spaces to align the outline properly, followed by an asterisk (*), and then the required number of spaces to create the outline effect.
5. The program exits after printing the hexagon outline.

### Conclusion

In this article, we learned how to print the outline of a hexagon shape using C++ and provided a complete code example. We explored the step-by-step process of printing the hexagon outline, including the upper triangle outline, middle area, and lower triangle outline. Now you have the knowledge to create and customize hexagon outlines in your C++ programs. Happy coding!