C++ program to print falling star triangle pattern

WBOY
Release: 2023-08-30 22:41:10
forward
605 people have browsed it

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.

grammar

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

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.

The Chinese translation of is: The Chinese translation of is: The translation of is: The translation of is: The Chinese translation of is: The Chinese translation of is:
Line number (i) Number of stars (j) Space(k)
177 0
266 1
3 5 2
4444 3
5 3 4
66 2 5
77 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.

algorithm

  • Read the number of lines as input n
    • For i from 1 to n, execute
    • For k ranging from 1 to (i - 1), execute
      • Display spaces (' ')
    • Finish
    • For j from 1 to (n - i 1), do the following
      • Display an asterisk followed by a space "*"
    • Finish
    • Move the cursor to the next line
  • Finish

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.

Example

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

Output

Downward Star Pattern using 10 number of lines: * * * * * * * * * * .* * * * * * * * * ..* * * * * * * * ...* * * * * * * ....* * * * * * .....* * * * * ......* * * * .......* * * ........* * .........*
Copy after login

Output (when n = 18)

Downward Star Pattern using 18 number of lines: * * * * * * * * * * * * * * * * * * .* * * * * * * * * * * * * * * * * ..* * * * * * * * * * * * * * * * ...* * * * * * * * * * * * * * * ....* * * * * * * * * * * * * * .....* * * * * * * * * * * * * ......* * * * * * * * * * * * .......* * * * * * * * * * * ........* * * * * * * * * * .........* * * * * * * * * ..........* * * * * * * * ...........* * * * * * * ............* * * * * * .............* * * * * ..............* * * * ...............* * * ................* * .................*
Copy after login

in conclusion

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!