C++ Program to Print Mirror Upper Star Triangle Pattern


When learning programming, displaying a star pattern makes looping concepts easier to comprehend. The star patterns are various designs that use asterisks to create hard or hollow triangles or diamond forms. This article will demonstrate a C++ right-aligned mirrored upper triangle pattern.

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

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

Six lines are there in this sample. So let's assume n = 6. There are stars and padding-related empty spaces for each line I as well. To exhibit the patterns, we must create a relationship between stars and empty areas.

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

In this problem, the number of stars follows (n – i + 1) but the blank spaces are increasing gradually. It follows (i – 1). To implement this, we need another loop for blank spaces only, before printing stars.

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 ( * )
    • end for
    • move the 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.

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 << "Mirrored Upper-Left Star Pattern using " << n << " number of lines:" << endl;
   solve( n );
}

Output

Mirrored Upper-Left Star Pattern using 10 number of lines:
* * * * * * * * * * 
. * * * * * * * * * 
. . * * * * * * * * 
. . . * * * * * * * 
. . . . * * * * * * 
. . . . . * * * * * 
. . . . . . * * * * 
. . . . . . . * * * 
. . . . . . . . * * 
. . . . . . . . . * 

Output (for n = 20)

Mirrored Upper-Left Star Pattern using 20 number of lines:
* * * * * * * * * * * * * * * * * * * *
. * * * * * * * * * * * * * * * * * * *
. . * * * * * * * * * * * * * * * * * *
. . . * * * * * * * * * * * * * * * * *
. . . . * * * * * * * * * * * * * * * *
. . . . . * * * * * * * * * * * * * * *
. . . . . . * * * * * * * * * * * * * *
. . . . . . . * * * * * * * * * * * * *
. . . . . . . . * * * * * * * * * * * *
. . . . . . . . . * * * * * * * * * * *
. . . . . . . . . . * * * * * * * * * *
. . . . . . . . . . . * * * * * * * * *
. . . . . . . . . . . . * * * * * * * *
. . . . . . . . . . . . . * * * * * * *
. . . . . . . . . . . . . . * * * * * *
. . . . . . . . . . . . . . . * * * * *
. . . . . . . . . . . . . . . . * * * *
. . . . . . . . . . . . . . . . . * * *
. . . . . . . . . . . . . . . . . . * *
. . . . . . . . . . . . . . . . . . . *

Conclusion

In every programming language, nested loops are taught using star patterns. The upperright triangle is displayed in this article as an example when the number of lines is provided. The number of lines with that many stars on each line will be shown. Stars are preceded by empty spaces to aid with alignment (due to a few limitations of online compilers, we print dots rather than spaces). You can test them locally by leaving gaps between sentences. It has also been demonstrated to determine the stars and empty spaces for the ith line using a tabular technique. Using this concept, we can easily modify the formula to show additional patterns.

Updated on: 13-Dec-2022

445 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements