Home >Backend Development >C++ >C++ program to print hollow right triangular star pattern
It is useful to display star patterns in different formats such as pyramid, square and diamond Commonly used in basic programming and logic construction. How many stars have we seen Number pattern issues when learning loop statements in programming. in the text, We will see how to print a hollow right triangle star pattern in C.
In this program we take row number n which will create a star pattern for n numbers Wire. Let's look at the same example.
* * * * * * * * * * * * * * * * * *
In this example, there are n rows. Each row has several stars, and at the beginning there are is a space. There are only two stars in the row except the first and last row, and There are spaces in between. The first row contains only one star and the last row Contains n stars.
Each line has n – i spaces, and then prints stars. For complete right-aligned triangles, we print the stars continuously. But here we print For the first and last positions, spaces are printed to make them hollow otherwise. in the final OK, it will print all the stars to make the border.
Due to some limitations of the online compiler, we added dots instead of spaces at the code. start. Otherwise, it will sometimes truncate the line and the output will become incorrect.
#include <iostream> using namespace std; void solve( int n ){ for ( int i = 1; i <= n; i++ ) { for ( int j = 1; j <= n - i; j++ ) { cout << "."; } for ( int k = 1; k <= i; k++ ) { if ( i != n ) { if ( k == 1 || k == i ) { cout << "*"; } else { cout << " "; } } else { cout << "*"; } } cout << "\n"; } } int main(){ int n = 7; cout << "Hollow Star Pattern for " << n << " lines." << endl; solve( n ); }
Hollow Star Pattern for 7 lines. ......* .....** ....* * ...* * ..* * .* * *******
Hollow Star Pattern for 18 lines. .................* ................** ...............* * ..............* * .............* * ............* * ...........* * ..........* * .........* * ........* * .......* * ......* * .....* * ....* * ...* * ..* * .* * ******************
The star pattern is easy to implement and helps to study the concept of loops in programming. In some articles we have seen techniques for printing star patterns such as pyramids, boxes etc. diamond. In this article we learned how to display a hollow pyramid in C. it takes Row number n, then displays a right-aligned hollow triangular star pattern. Each line has After a few spaces there are a few stars. Spaces are used for alignment (due to limitations For the online compiler, we use dots instead of spaces for alignment). For the last line, it Prints all stars but for other rows it prints stars only at first and last row position Pillar. This creates a hollow triangle.
The above is the detailed content of C++ program to print hollow right triangular star pattern. For more information, please follow other related articles on the PHP Chinese website!