C++ Program to Print Hollow Square with Diagonals Star Pattern

Write a C++ program to print the hollow square with both the diagonals of star pattern using for loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, side;
     
    cout << "Enter Side of a Hollow Square with Diagonals = ";
    cin >> side;

    cout << "Hollow Square Star With Diagonals Pattern\n"; 
    for(i = 1; i <= side; i++)
    {
    	for(j = 1; j <= side; j++)
		{
            if (i == 1 || i == side || i == j || 
                j == 1 || j == side || j == side - i + 1) 
            {
                cout << "* ";
            }
           	else {
                   cout << "   ";
               } 
        }
        cout << "\n";
    }		
 	return 0;
}
C++ Program to Print Hollow Square with Diagonals Star Pattern

This C++ example prints the hollow square with the diagonals pattern of a given character using a while loop.

#include<iostream>
using namespace std;

int main()
{
	int i, j, side;
    char ch;
     
    cout << "Enter Side of a Hollow Square with Diagonals = ";
    cin >> side;

    cout << "Enter Symbol for Hollow Square with Diagonals = ";
    cin >> ch;

    cout << "Hollow Square With Diagonals Pattern\n"; 
    for(i = 1; i <= side; i++)
    {
    	for(j = 1; j <= side; j++)
		{
            if (i == 1 || i == side || i == j || 
                j == 1 || j == side || j == side - i + 1) 
            {
                cout << ch << " ";
            }
           	else {
                   cout << "  ";
               } 
        }
        cout << "\n";
    }		
 	return 0;
}
Enter Side of a Hollow Square with Diagonals = 16
Enter Symbol for Hollow Square with Diagonals = #
Hollow Square With Diagonals Pattern
# # # # # # # # # # # # # # # # 
# #                         # # 
#   #                     #   # 
#     #                 #     # 
#       #             #       # 
#         #         #         # 
#           #     #           # 
#             # #             # 
#             # #             # 
#           #     #           # 
#         #         #         # 
#       #             #       # 
#     #                 #     # 
#   #                     #   # 
# #                         # # 
# # # # # # # # # # # # # # # # 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.