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.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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 -
#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 ); }
Complete Square Star Pattern of 10 lines: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * *
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 -
Let's see the correct algorithm for better understanding.
#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 ); }
Hollow Square Star Pattern of 10 lines: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Hollow Square Star Pattern of 18 lines: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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!