Print simple patterns like below using single line of code under loop.
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() Used to set fill character in a stream. Here we use it to fill remaining n-i-1 places with space (or ‘ ‘) in n columns.
* ** *** **** ***** ******
Please refer below post for one more approach.
Print pattern using only one loop | Set 2 (Using Continue)
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks( We know you do! ) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print a pattern without using any loop
- Print the pattern by using one loop | Set 2 (Using Continue Statement)
- Printing triangle star pattern using a single loop
- std::setbase, std::setw , std::setfill in C++
- iomanip setw() function in C++ with Examples
- Print 1 to 100 in C++, without loop and recursion
- How will you print numbers from 1 to 100 without using loop?
- Print a character n times without using loop, recursion or goto in C++
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Program to print pattern
- Pattern to print X in a rectangular box
- Print the given pattern recursively
- Program to print the given H Pattern
- Print the following pyramid pattern
- Program to print the pattern "GFG"
- Program to print the given Z Pattern
- Program to print the pattern 'G'
- Program to print the pattern ‘D’
- Print a String in wave pattern
- Program to print interesting pattern