C++ Program to Print Square Star Pattern



Designing patterns using special characters like stars (asterisks) is one of the most common types of programs to understand looping concepts. There are plenty of other star pattern programs which are very common in nature. The star pyramid is fairly simple but much more effective to understand looping statements and their conditions. In this article, we will see how to display a square pattern using stars in C++. Initially the complete square and then the hollow square.

Displaying Complete Square Pattern

* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *

There are seven lines. So consider n = 7. Since we are trying to print the complete square. Each row will print n number of stars. The corresponding table to frame the formula is like below −

Line number (i) Star Count (j)
1 7
2 7
3 7
4 7
5 7
6 7
6 7
7 7

Here star count is constant for all rows. Let us see the algorithm for this −

Algorithm

  • read number of lines as input n
    • for i ranging from 1 to n, do
    • for j ranging from 1 to n, do
      • print asterisk
    • end for
    • move the cursor to the next line
  • end for

Example

#include <iostream>
using namespace std;
void solve( int n ){
   int i, j, k;
   for( i = 1; i <= n; i++ ) {
      for( j = 1; j <= n; j++ ) {
         cout << "* ";
      }
      cout << endl;
   }
}
int main(){
   int n = 10;
   cout << "Complete Square Star Pattern of " << n << " lines:" <<
       endl;
   solve( n );
}

Output

Complete Square Star Pattern of 10 lines:
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 

Displaying Hollow Square Pattern

* * * * * * *
*           *
*           *
*           *
*           *
*           *
* * * * * * *

In this pattern, the first and the last row has all n stars. The remaining lines have two stars, one at the beginning, and another at the end. This can be elaborated in the following few points −

  • When line number ‘i' is 1 or n, print n stars
  • Otherwise, for the first and last column, print * rest fill with blank space ‘ ‘.

Let us see the proper algorithm for better understanding.

Algorithm

  • read number of lines as input n
  • for i ranging from 1 to n, do
    • for j ranging from 1 to n, do
      • if i is 1 or n, then
        • print asterisk
      • otherwise when j = 1 or n, then
        • print asterisk
      • otherwise
        • print blank space
      • end if
    • end for
    • move cursor to the next line
  • end for

Example

#include <iostream>
using namespace std;
void solve( int n ){
   int i, j;
   for( i = 1; i <= n; i++ ) {
      for( j = 1; j <= n; j++ ) {
         if( i == 1 || i == n ) {
            cout << "* ";
         } else if( j == 1 || j == n ) {
            cout << "* ";
         } else {
            cout << "  ";
         }
      }
      cout << endl;
   }
}
int main(){
   int n = 10;
   cout << "Hollow Square Star Pattern of " << n << " lines:" << endl;
   solve( n );
}

Output

Hollow Square Star Pattern of 10 lines:
* * * * * * * * * * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
* * * * * * * * * * 

Output (n = 18)

Hollow Square Star Pattern of 18 lines:
* * * * * * * * * * * * * * * * * *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
* * * * * * * * * * * * * * * * * *

Conclusion

Like triangular patterns using stars, we can also display some other structures of patterns using simple logic programming. In this article we have shown, how to display a square pattern, one is complete and another one is hollow. For a complete pattern, each line must have n number of stars. The star count is constant. On the other hand, for a hollow square, the first line and the last line will have n stars but the remaining lines will have only two stars. The first and the last characters will be the stars, intermediate characters are spaces for padding.


Advertisements