Home > Article > Backend Development > C++ program to print falling star triangle pattern
Make it easier to understand circular ideas by printing a star design. Asterisk is used for Various star patterns form full or hollow triangle or rhombus forms. at this In this article, we will show how to create a center-aligned descending triangle in C.
The following table will contain the logic we created to print the stars. The following table can Help us understand.
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
7 lines are shown here. For each row i, there are (n – i 1) stars. However, every The rows have some padding, and here the padding is decreasing with each row. And stars also have Constant filling. We can do this by printing "*" (asterisk followed by space) Instead of just printing "*". The table shows the number of spaces and stars and their relationship the value of i.
Line number (i) | Number of stars (j) | Space(k) | ||
---|---|---|---|---|
1 | The Chinese translation of7 | is:7 | 0 | |
2 | The Chinese translation of6 | is:6 | 1 | |
3 | 5 | 2 | ||
4 | is:4 | The translation of4 | is:4 | 3 |
5 | 3 | 4 | ||
6 | is:6 | 2 | 5 | |
7 | is:7 | 1 | 6 |
Here, the number of stars in each row i is (n – i 1). The number of spaces is as follows (i – 1). let us Check out Algorithms to understand this concept.
We are testing this by replacing spaces with dots (.) before each line. because of U.S Online compilers sometimes truncate lines and eliminate spaces before and after each line.
#include <iostream> using namespace std; void solve( int n ){ int i, j, k; for( i = 1; i <= n; i++ ) { for( k = 1; k <= (i - 1); k++ ) { cout << "."; } for( j = 1; j <= (n - i + 1); j++ ) { cout << "* "; } cout << endl; } } int main(){ int n = 10; cout << "Downward Star Pattern using " << n << " number of lines:" << endl; solve( n ); }
Downward Star Pattern using 10 number of lines: * * * * * * * * * * .* * * * * * * * * ..* * * * * * * * ...* * * * * * * ....* * * * * * .....* * * * * ......* * * * .......* * * ........* * .........*
Downward Star Pattern using 18 number of lines: * * * * * * * * * * * * * * * * * * .* * * * * * * * * * * * * * * * * ..* * * * * * * * * * * * * * * * ...* * * * * * * * * * * * * * * ....* * * * * * * * * * * * * * .....* * * * * * * * * * * * * ......* * * * * * * * * * * * .......* * * * * * * * * * * ........* * * * * * * * * * .........* * * * * * * * * ..........* * * * * * * * ...........* * * * * * * ............* * * * * * .............* * * * * ..............* * * * ...............* * * ................* * .................*
We designed programs to print star patterns to learn almost any programming language
Nested for loop syntax. In this article, we have covered how to print a center-aligned Descending triangle pattern. Asterisks are placed to print triangles, spaces are used Align the triangle to the center (due to some limitations of the online compiler, we print dots instead of spaces). You can test them locally by putting a gap in between Use dots instead of spaces to indicate gaps). You can test them locally, just put a gap in between sentence. Also prove that the i-th row of stars and empty space Can be found using tabular techniques. Using this concept, we can easily modify A way for formulas to display additional patterns. Making simple changes from the table can help show Different triangle patterns. Sometimes removing the space will make the triangle left aligned.The above is the detailed content of C++ program to print falling star triangle pattern. For more information, please follow other related articles on the PHP Chinese website!