C++ Program to Print 8 Star Pattern

Write a C++ program to print 8 star pattern using for loop.

#include<iostream>
using namespace std;

int main()
{
	int rows;

	cout << "Please Enter 8 Pattern Rows = ";
	cin >> rows;

	cout << "Printing 8 Pattern of Stars\n";

	for (int i = 1; i <= rows * 2 - 1; i++)
	{
		if (i == 1 || i == rows || i == rows * 2 - 1)
		{
			for (int j = 1; j <= rows; j++)
			{
				if (j == 1 || j == rows)
				{
					cout << " ";
				}
				else
				{
					cout << "*";
				}
			}
		}
		else
		{
			for (int k = 1; k <= rows; k++)
			{
				if (k == 1 || k == rows)
				{
					cout << "*";
				}
				else
				{
					cout << " ";
				}
			}
		}
		cout << "\n";
	}
}
C++ Program to Print 8 Star Pattern

C++ Program to Print 8 Star Pattern using a while loop

#include<iostream>
using namespace std;

int main()
{
	int rows, i, j, k;

	cout << "Please Enter 8 Pattern Rows = ";
	cin >> rows;

	cout << "Printing 8 Pattern of Stars\n";
	i = 1;
	while (i <= rows * 2 - 1)
	{
		if (i == 1 || i == rows || i == rows * 2 - 1)
		{
			j = 1;
			while (j <= rows)
			{
				if (j == 1 || j == rows)
				{
					cout << " ";
				}
				else
				{
					cout << "*";
				}
				j++;
			}
		}
		else
		{
			k = 1;
			while (k <= rows)
			{
				if (k == 1 || k == rows)
				{
					cout << "*";
				}
				else
				{
					cout << " ";
				}
				k++;
			}
		}
		cout << "\n";
		i++;
	}
}
Please Enter 8 Pattern Rows = 10
Printing 8 Pattern of Stars
 ******** 
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
 ******** 
*        *
*        *
*        *
*        *
*        *
*        *
*        *
*        *
 ******** 

This C+++ program prints the digital or digit 8 pattern of stars using the do while loop.

#include<iostream>
using namespace std;

int main()
{
	int rows, i, j, k;

	cout << "Please Enter 8 Pattern Rows = ";
	cin >> rows;

	cout << "Printing 8 Pattern of Stars\n";
	i = 1;
	do
	{
		if (i == 1 || i == rows || i == rows * 2 - 1)
		{
			j = 1;
			do
			{
				if (j == 1 || j == rows)
				{
					cout << " ";
				}
				else
				{
					cout << "*";
				}
			} while (++j <= rows);
		}
		else
		{
			k = 1;
			do
			{
				if (k == 1 || k == rows)
				{
					cout << "*";
				}
				else
				{
					cout << " ";
				}
			} while (++k <= rows);
		}
		cout << "\n";
	} while (++i <= rows * 2 - 1);
}
Please Enter 8 Pattern Rows = 13
Printing 8 Pattern of Stars
 *********** 
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
 *********** 
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
*           *
 *********** 

In this C++ example, star8Pattern function allows rows and character and prints the digit 8 pattern of given character.

#include<iostream>
using namespace std;

void Pattern8(int rows, char ch)
{
	for (int i = 1; i <= rows * 2 - 1; i++)
	{
		if (i == 1 || i == rows || i == rows * 2 - 1)
		{
			for (int j = 1; j <= rows; j++)
			{
				if (j == 1 || j == rows)
				{
					cout << " ";
				}
				else
				{
					cout << ch;
				}
			}
		}
		else
		{
			for (int k = 1; k <= rows; k++)
			{
				if (k == 1 || k == rows)
				{
					cout << ch;
				}
				else
				{
					cout << " ";
				}
			}
		}
		cout << "\n";
	}
}
int main()
{
	int rows;
	char ch;

	cout << "Enter Character for 8 Pattern = ";
	cin >> ch;

	cout << "Please Enter 8 Pattern Rows = ";
	cin >> rows;

	cout << "Printing 8 Pattern of Stars\n";
	Pattern8(rows, ch);
}
Enter Character for 8 Pattern = #
Please Enter 8 Pattern Rows = 15
Printing 8 Pattern of Stars
 ############# 
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
 ############# 
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
#             #
 ############# 

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.