
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- for k ranging from 1 to (i - 1), 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 <= (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.
- Related Articles
- Java Program to Print Mirror Upper Star Triangle Pattern
- Swift Program to Print Mirror Upper Star Triangle Pattern
- Golang Program To Print Mirror Upper Star Triangle Pattern
- Haskell Program to Print Mirror Upper Star Triangle Pattern
- C++ Program to Print Upper Star Triangle Pattern
- Java Program to Print Upper Star Triangle Pattern
- Swift program to Print Upper Star Triangle Pattern
- Golang Program To Print Upper Star Triangle Pattern
- Haskell Program to Print Upper Star Triangle Pattern
- Java Program to Print Mirror Lower Star Triangle Pattern
- Haskell Program to Print Mirror Lower Star Triangle Pattern
- Swift program to Print Mirror Lower Star Triangle Pattern
- Golang Program To Print Mirror Lower Star Triangle Pattern
- C++ Program to Print Right Triangle Star Pattern
- C++ Program to Print Left Triangle Star Pattern
