Write a C++ program to print a mirrored right triangle star pattern below using a single line of code under a circle.
Examples:
Input : 5 Output : * ** *** **** ***** Input : 6 Output : * ** *** **** ***** ******
setw(n) Creates n columns and fills these n columns from right. We fill i of them with a given character, here we create a string with i asterisks using string constructor.
setfill() wont to set fill character during a stream. Here we use it to fill remaining n-i-1 places with space (or ‘ ‘) in n columns.
// CPP program to print a pattern using only // one loop. is header file for stfill() // and setw() #include #include using namespace std; void generatePattern(int n) { // Iterate for n lines for (int i=1 ; i<=n ; i++) cout << setfill(' ') << setw(n) << string(i, '*') << endl; // Remove multi-line commenting characters below // to get PATTERN WITH CHARACTERS IN LEFT AND // SPACE IN RIGHT /* for (int i=1 ; i<=n ; i++) cout << left << setfill(' ') << setw(n) << string(i,'*') << endl; */ } // Driver code int main() { int n = 6; generatePattern(n); return 0; }
Output:
* ** *** **** ***** ******