- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Print Hollow Right Triangle Star Pattern
Displaying star patterns in different formats like pyramids, squares, and diamonds is very common in fundamental programming and logic building. We have seen several stars and number pattern problems while learning looping statements in programming. In this article, we will see how to print a hollow right triangle star pattern in C++.
In this program, we take the line number n this will create a star pattern for n number of lines. Let us see the example for the same.
Hollow Right Star Pattern
* * * * * * * * * * * * * * * * * *
Here in this example, there are n lines. Each line has a few stars and at the beginning, there are blank spaces. Except for the first and last lines, there are only two stars in that line, and in between, there are blank spaces. The first line contains only one star and the last line contains n number of stars.
For each line, there are n – i number of blank spaces and after that, the stars are printed. For complete right-aligned triangles, we print stars consecutively. But here we are printing for first and last places, otherwise blank spaces are printed to make it hollow. In the final line, it prints all stars to make the border.
Algorithm
- Read n as input
- for i ranging from 1 to n, do
- for j ranging from 1 to n - i, do
- put blank space
- end for
- for k ranging from 1 to i, do
- if i is not the same as n, then
- if k = 1 or k = i, then
- display '*'
- otherwise
- display blank space
- end if
- if k = 1 or k = i, then
- otherwise
- display '*'
- end if
- if i is not the same as n, then
- end for
- move the cursor to the next line
- for j ranging from 1 to n - i, do
- end for
Due to a few limitations of online compilers, we have added dots instead of spaces at the beginning. Otherwise, it truncates the line sometimes, and the output becomes incorrect.
Example
#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 ); }
Output
Hollow Star Pattern for 7 lines. ......* .....** ....* * ...* * ..* * .* * *******
Output (With n = 18)
Hollow Star Pattern for 18 lines. .................* ................** ...............* * ..............* * .............* * ............* * ...........* * ..........* * .........* * ........* * .......* * ......* * .....* * ....* * ...* * ..* * .* * ******************
Conclusion
Star patterns are easy to implement and helpful to study looping concepts in programming. In some articles, we have seen techniques to print star patterns like pyramids, boxes, and diamonds. In this article, we have seen how to display hollow pyramids in C++. It takes the number of lines n, then displays the right-aligned hollow triangle star pattern. Each line has a few stars after a few blank spaces. Blank spaces are used for alignment (Due to limitations of online compilers, we have used dots instead of spaces for alignment). For the last line, it prints all stars, but for other lines, it just prints stars when at the place of the first and last column. Thus the hollow triangle is formed.