Home > Backend Development > C++ > body text

C++ program to print square star pattern

WBOY
Release: 2023-08-27 16:57:06
forward
1708 people have browsed it

C++ program to print square star pattern

Design pattern using special characters like asterisk (asterisk) is one of the most common patterns Types of programs that understand the concept of loops. There are many other star patterns A very common procedure in nature. The Star Pyramid is fairly simple but packed with content Understand loop statements and their conditions more efficiently. In this article we will Learn how to display a square pattern using a star in C. Initially the complete square and Then there are the hollow squares.

Show complete square pattern

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

There are seven lines in total. So consider n = 7. Because we are trying to print a complete square. Each line will print n stars. The corresponding table of the framework formula is as follows under -

Line number (i) Number of stars (j)
1 7
2 7
3 7
4 7
5 7
6 7
6 7
7 7

The number of stars in all rows here is constant. Let’s look at this algorithm -

algorithm

  • Read the number of lines as input n
    • For i from 1 to n, execute
    • For j ranging from 1 to n, execute
      • Print asterisk
    • Finish
    • Move the cursor to the next line
  • Finish

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

Output

Complete Square Star Pattern of 10 lines:
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
Copy after login

Display hollow square pattern

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

In this mode, both the first and last rows have n stars. The remaining rows have two stars, One at the beginning and the other at the end. This can be explained in detail through the following items point -

  • When the line number "i" is 1 or n, print n stars
  • Otherwise, for the first and last columns, print * and fill the rest with spaces " ".

Let's see the correct algorithm for better understanding.

algorithm

  • Read the number of lines as input n
  • For i from 1 to n, execute
    • For j ranging from 1 to n, execute
      • If i is 1 or n, then
        • Print asterisk
      • Otherwise when j = 1 or n, then
        • Print asterisk
      • otherwise
        • Print blank
      • If it ends
    • Finish
    • Move the cursor to the next line
  • Finish

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

Output

Hollow Square Star Pattern of 10 lines:
* * * * * * * * * * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
*                 * 
* * * * * * * * * * 
Copy after login

Output (n = 18)

Hollow Square Star Pattern of 18 lines:
* * * * * * * * * * * * * * * * * *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
*                                 *
* * * * * * * * * * * * * * * * * *
Copy after login

in conclusion

Just like using the triangle pattern of stars, we can also display some other pattern structures Use simple logic programming. In this article we show how to display a square Patterns, one is complete and the other is hollow. For a complete pattern, each row must There are n stars. The number of stars is constant. On the other hand, for a hollow square, The first and last rows will have n stars, but the remaining rows will have only two stars Star. The first and last characters will be stars and the middle character will be a space for filling.

The above is the detailed content of C++ program to print square star pattern. 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