
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Right Triangle Star Pattern
Asterisks "*" are used in star patterns to represent various shapes, such as triangles, diamonds, hollow shapes, etc. Star patterns are the names for these forms. This tutorial will illustrate how to use C++ to display the left triangle star pattern where the triangle is aligned to the left. Here, we accept as an input the number of lines in the star design. For that many lines, the pattern will be printed.
The following table will contain the logic we create to print stars. The following table can help us understand.
* * * * * * * * * * * * * * * * * * * * *
In this example, there are 6 lines. So consider n = 6. For each line ?i' there are stars and also blank spaces for padding. We must formulate relation for stars and for blank spaces to display the patterns
Line number (i) | Star Count (j) | Blank Space (k) |
---|---|---|
1 | 1 | 5 |
2 | 2 | 4 |
3 | 3 | 3 |
4 | 4 | 2 |
5 | 5 | 1 |
6 | 6 | 0 |
In this problem, the number of stars follows the line number ?i' but the blank spaces are reducing gradually. It follows (n - i). 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 (n - i), do
- display blank space (' ')
- end for
- for j ranging from 1 to i, do
- display asterisk ( * )
- end for
- move the cursor to the next line
- for k ranging from 1 to (n - i), do
- 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 <= (n - i); k++ ) { cout << ". "; } for( j = 1; j <= i; j++ ) { cout << "* "; } cout << endl; } } int main(){ int n = 10; cout << "Left Star Pattern using " << n << " number of lines:" << endl; solve( n ); }
Output
Left Star Pattern using 10 number of lines: . . . . . . . . . * . . . . . . . . * * . . . . . . . * * * . . . . . . * * * * . . . . . * * * * * . . . . * * * * * * . . . * * * * * * * . . * * * * * * * * . * * * * * * * * * * * * * * * * * * *
Output (for n = 18)
Left Star Pattern using 18 number of lines: . . . . . . . . . . . . . . . . . * . . . . . . . . . . . . . . . . * * . . . . . . . . . . . . . . . * * * . . . . . . . . . . . . . . * * * * . . . . . . . . . . . . . * * * * * . . . . . . . . . . . . * * * * * * . . . . . . . . . . . * * * * * * * . . . . . . . . . . * * * * * * * * . . . . . . . . . * * * * * * * * * . . . . . . . . * * * * * * * * * * . . . . . . . * * * * * * * * * * * . . . . . . * * * * * * * * * * * * . . . . . * * * * * * * * * * * * * . . . . * * * * * * * * * * * * * * . . . * * * * * * * * * * * * * * * . . * * * * * * * * * * * * * * * * . * * * * * * * * * * * * * * * * *
Conclusion
When learning programming in any language, star patterns are used to teach nested loops. This article demonstrates how to display the right-aligned triangle when the number of lines is given. It will display how many lines there are with that many stars on each line. Blank spaces are displayed before stars for alignment purposes (online compilers caused us to print dots rather than spaces). When testing them locally, you can test them with empty gaps. A tabular approach has also been investigated to determine the stars and empty spaces for the ith line. We may easily change the formula to demonstrate additional patterns using this idea.