Solid and Hollow Rectangle Star Pattern in C++
C++ Program for Solid and Hollow Rectangle Star Pattern
Here we will see how loops can be used for printing solid and hollow rectangle star patterns.Different patterns can be printed using the same concept which is nested looping i.e. loop within a loop.
Now lets see the code for Solid as well as Hollow Rectangle star pattern
Code for solid Rectangle star pattern
// C++ program
//solid Rectangle star pattern
#include <iostream>
using namespace std;
// Function for Solid Rectangle
void solidRectangle(int row,int col)
{
int i, j;
for (i=1; i<=row;i++)
{
for (j=1;j<=col; j++) // Printing stars after spaces
cout << "*";
cout << endl;
}
}
// main program
int main()
{
int row = 7,col= 9;
cout << "\nSolid Rectangle:\n";
solidRectangle(row,col);
return 0;
}
Output:
Solid Rectangle:
*********
*********
*********
*********
*********
*********
*********
C++ code for hollow rectangle star pattern
//C++ program
//hollow Rectangle Star Pattern
#include<iostream>
using namespace std;
// Function for hollow rectangle
void hollowPattern(int row,int col)
{
int i, j;
for (i=1; i<=row; i++)
{
if (i==1 || i==row) // printing stars for each solid rows
{
for (j=1; j<=col; j++)
cout << "*";
}
else // stars for hollow rows
{
for (j=1; j<=col; j++)
if (j==1 || j==col)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}
// main program
int main()
{
int row = 7,col= 9;
hollowPattern(row,col);
return 0;
}
Output:
*********
* *
* *
* *
* *
* *
*********