# C++ Program to Print Upward Triangle Star Pattern

When learning programming, displaying a star pattern makes looping concepts easier to get. The star patterns are various designs that use asterisks to create hard or hollow triangles or diamond forms. This article will demonstrate a C++ implementation for an upward triangle that is center-aligned.

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

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


Here 7 lines are shown. For each line i, there are i number of stars. However, each line has some padding. 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 1 6
2 2 5
3 3 4
4 4 3
5 5 2
6 6 1
7 7 0

Here star count follows the line number ‘i'. There is (n – i) number of blank spaces on each line. 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 (n - i), do
• display blank space (' ')
• end for
• for j ranging from 1 to i, do
• display asterisk folloed by a blank space '* '
• end for
• move cursor to the next line
• end for

Since our online compilers sometimes truncate the lines and remove the blank spaces before and after each line, we are testing by putting dots (.) in the place of blank spaces before 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 <= (n - i); k++ ) {
cout << ".";
}
for( j = 1; j <= i; j++ ) {
cout << "* ";
}
cout << endl;
}
}
int main(){
int n = 10;
cout << "Upward Star Pattern using " << n << " number of lines:" << endl;
solve( n );
}


### Output

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


### Output (for n = 15)

Upward Star Pattern using 15 number of lines:
..............*
.............* *
............* * *
...........* * * *
..........* * * * *
.........* * * * * *
........* * * * * * *
.......* * * * * * * *
......* * * * * * * * *
.....* * * * * * * * * *
....* * * * * * * * * * *
...* * * * * * * * * * * *
..* * * * * * * * * * * * *
.* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *


## Conclusion

Nested loops are explained using star patterns throughout all programming languages. In this article, we have discussed how to print an upward triangular pattern which is center aligned. To make alignment easier, empty spaces come before stars (due to a few limitations of online compilers, we print dots rather than spaces). By inserting spaces in the middle of sentences, you may test them locally. It has also been shown that a tabular approach can be used to identify the stars and empty spaces for the ith line. We may easily change the formula to demonstrate additional patterns using this idea.

Updated on: 13-Dec-2022

250 Views