Home > Article > Backend Development > C++ program to print 8 star patterns
It is useful to display star patterns in different formats such as pyramid, square and diamond Commonly used in basic programming and logic construction. How many stars have we seen Number pattern issues when learning loop statements in programming. in the text, We will show the number eight (8) made of stars in C.
In this program, we take line number n, which is the size of the upper half of 8. The lower half will be the same. The eight patterns are as follows
* * * * * * * * * * * * * * * * * * * * *
In the above example, the number of rows, n = 5. For the first five rows, the first half of 8 is is taking shape. When line numbers are 1, n, and n*2, asterisks are printed in Continuous fashion. For the rest of the other lines, only two stars are printed. let us see algorithm for better understanding.
#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 ); }
Eight Pattern for 7 lines. ***** * * * * * * * * * * ***** * * * * * * * * * * *****
Eight Pattern for 12 lines. ********** * * * * * * * * * * * * * * * * * * * * ********** * * * * * * * * * * * * * * * * * * * * **********
The display of numeric modes is one of the more typical problems encountered when using Learn a programming language. This article demonstrates how to use asterisks to display Number 8. (Star). For the number 8, it multiplies the number of rows by 2 to generate n*2 row pattern. Both the upper and lower halves are composed of n lines. Additionally, the width of the pattern is of size n.
The above is the detailed content of C++ program to print 8 star patterns. For more information, please follow other related articles on the PHP Chinese website!