C++ Program to Print 8 Star Pattern


Displaying star patterns in different formats like pyramids, squares, and diamonds is very common in fundamental programming and logic building. We have seen several stars and number pattern problems while learning looping statements in programming. In this article, we will display the number Eight (8) made with stars in C++.

In this program, we take the line number n which is the size of the upper half of the eight. The lower half will be identical. The eight-pattern will look like below

Eight-Pattern with stars

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

In the above example, the number of lines, n = 5. For the first five lines, the upper half of 8 is being formed. When the line numbers are 1, n, and n*2, the stars are printed in a consecutive fashion. For the rest of the other lines, only two stars are printed. Let us see the algorithm for a better understanding.

Algorithm

  • Take input n as the number of lines for the upper half of 8
  • for i ranging from 1 to 2n - 1, do
    • if i is 1 or n or i is n*2, then
      • for j ranging from 1 to n, do
        • if j is 1 or n, then
          • display blank space
        • otherwise
          • display asterisk (*)
        • end if
      • end for
    • otherwise
      • for k ranging from 1 to n, do
        • if k is 1 or n, then
          • display asterisk (*)
        • otherwise
          • display blank space
        • end if
      • end for
    • end if
    • move cursor to the next line
  • end for

Example

#include <iostream>
using namespace std;
void solve( int n ){
   for ( int i = 1; i <= n * 2 - 1; i++ ) {
      if ( i == 1 || i == n || i == n * 2 - 1 ) {
         for ( int j = 1; j <= n; j++ ) {
            if ( j == 1 || j == n ) {
               cout << " ";
            } else {
               cout << "*";
            }
         }
      } else {
         for ( int k = 1; k <= n; k++ ) {
            if ( k == 1 || k == n ) {
               cout << "*";
            } else {
               cout << " ";
            }
         }
      }
      cout << "\n";
   }
}
int main(){
   int n = 7;
   cout << "Eight Pattern for " << n << " lines." << endl;
   solve( n );
}

Output

Eight Pattern for 7 lines.
 ***** 
*     *
*     *
*     *
*     *
*     *
 ***** 
*     *
*     *
*     *
*     *
*     *
 ***** 

Output (With n = 12)

Eight Pattern for 12 lines.
 ********** 
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
 ********** 
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
 ********** 

Conclusion

The display of numerical patterns is one of the more typical issues encountered when learning programming languages. This article demonstrated how to use asterisks to display the number 8. (stars). For the number 8, it multiplies the number of lines by two to produce n*2 lines of the pattern. Both the upper and lower halves are made up of n lines. Additionally, the pattern's breadth is of size n.

Updated on: 13-Dec-2022

684 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements