Home > Backend Development > C++ > body text

C++ program to print 8 star patterns

WBOY
Release: 2023-09-06 17:49:06
forward
1322 people have browsed it

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

Eight patterns with stars

 * * *
*     *
*     *
*     *
 * * *
*     *
*     *
*     *
 * * *
Copy after login

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.

algorithm

  • Enter n as the number of lines in the upper half of 8
  • For i ranging from 1 to 2n - 1, execute
    • If i is 1 or n or i is n*2, then
      • For j ranging from 1 to n, execute
        • If j is 1 or n, then
          • Display blank
        • otherwise
          • Show asterisk (*)
        • If it ends
      • Finish
    • otherwise
      • For k from 1 to n, execute
        • If k is 1 or n, then
          • Show asterisk (*)
        • otherwise
          • Display blank
        • If it ends
      • Finish
    • If it ends
    • Move the cursor to the next line
  • Finish

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 );
}
Copy after login

Output

Eight Pattern for 7 lines.
 ***** 
*     *
*     *
*     *
*     *
*     *
 ***** 
*     *
*     *
*     *
*     *
*     *
 ***** 
Copy after login

Output (n = 12)

Eight Pattern for 12 lines.
 ********** 
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
 ********** 
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
*          *
 ********** 
Copy after login

in conclusion

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template