C++ Program to Print Downward Star Triangle Pattern


Understanding looping ideas is made simpler by printing a star design. Asterisks are used in a variety of star patterns to form complete or hollow triangles or diamond forms. In this post, we will show how to create a center-aligned descending triangle in C++.

The following table will contain the logic we create to print stars. The following table can help us understand.

Syntax

* * * * * * *
 * * * * * *
  * * * * *
   * * * *
    * * *
     * *
      *

Here 7 lines are shown. For each line i, there are (n – i + 1) number of stars. However, each line has some padding, and here the padding is decreasing in each line. And stars also have constant padding. We can achieve this by printing ‘* ‘ (star followed by a blank space) instead of printing only ‘*’. The table shows the space and star count and their relation with the value of i.

Line number (i) Star Count (j) Blank Space (k)
1 7 0
2 6 1
3 5 2
4 4 3
5 3 4
6 2 5
7 1 6

Here star count follows (n – i + 1) for each line i. The number of blanks follows (i – 1). Let us see the algorithm to understand this concept.

Algorithm

  • read number of lines as input n
    • for i ranging from 1 to n, do
    • for k ranging from 1 to (i - 1), do
      • display blank space (' ')
    • end for
    • for j ranging from 1 to (n - i + 1), do
      • display asterisk followed by a blank space '* '
    • end for
    • move the cursor to the next line
  • end for

We are testing by substituting dots (.) for the blank spaces before each line because our online compilers occasionally truncate the lines and eliminate the blank spaces before and after each line.

Example

#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 );
}

Output

Downward Star Pattern using 10 number of lines:
* * * * * * * * * * 
.* * * * * * * * * 
..* * * * * * * * 
...* * * * * * * 
....* * * * * * 
.....* * * * * 
......* * * * 
.......* * * 
........* * 
.........* 

Output (for n = 18)

Downward Star Pattern using 18 number of lines:
* * * * * * * * * * * * * * * * * * 
.* * * * * * * * * * * * * * * * * 
..* * * * * * * * * * * * * * * * 
...* * * * * * * * * * * * * * * 
....* * * * * * * * * * * * * * 
.....* * * * * * * * * * * * * 
......* * * * * * * * * * * * 
.......* * * * * * * * * * * 
........* * * * * * * * * * 
.........* * * * * * * * * 
..........* * * * * * * * 
...........* * * * * * * 
............* * * * * * 
.............* * * * * 
..............* * * * 
...............* * * 
................* * 
.................* 

Conclusion

We design 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, downward triangle pattern. Stars are placed to print the triangle, and blank spaces are used to align the triangle into the center (due to a few limitations of online compilers, we print dots rather than spaces). You can test them locally by putting gaps in the middle of sentences. It has also been demonstrated that the stars and empty spaces for the ith line can be found using a tabular technique. Using this concept, we can easily modify the formula to show additional patterns. A simple change from the table can help to display different modes of triangles. Sometimes removing blank spaces will make the triangle leftaligned.

Updated on: 13-Dec-2022

788 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements