Print pattern using only one loop | Set 1 (Using setw) Last Updated : 02 Nov, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Print simple patterns like below using single line of code under loop. Examples: Input : 5Output : * ** *** *********Input : 6Output : * ** *** **** ***********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. CPP // CPP program to print a pattern using only // one loop. <iomanip> is header file for stfill() // and setw() #include<iostream> #include<iomanip> 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; } Java public class PatternPrinting { // Function to generate and print the pattern public static void generatePattern(int n) { for (int i = 1; i <= n; i++) { // Use printf to format the output with spaces on the left // and asterisks on the right, to create the pattern. System.out.printf("%" + n + "s%n", "*".repeat(i)); } } public static void main(String[] args) { int n = 6; // Define the number of lines in the pattern generatePattern(n); // Call the pattern generation function } } Python3 #Python equivalent #include <iostream> is replaced by print() function #include <iomanip> is replaced by string.ljust() def generatePattern(n): # Iterate for n lines for i in range(1, n+1): print(str('*'*i).rjust(n)) # Remove multi-line commenting characters below # to get PATTERN WITH CHARACTERS IN LEFT AND # SPACE IN RIGHT '''for i in range(1, n+1): print(str('*'*i).ljust(n))''' # Driver code if __name__ == '__main__': n = 6 generatePattern(n) C# // C# program to print a pattern using only // one loop. System namespace is used for Console // class. using System; public class GFG { public static void GeneratePattern(int n) { // Iterate for n lines for (int i = 1; i <= n; i++) Console.WriteLine("{0," + n + "}", new String('*', i)); // Remove multi-line commenting characters below // to get PATTERN WITH CHARACTERS IN LEFT AND // SPACE IN RIGHT /* for (int i=1 ; i<=n ; i++) Console.WriteLine("{0,-" + n + "}", new String('*', i)); */ } // Driver code public static void Main() { int n = 6; GeneratePattern(n); } } JavaScript // JS program to print a pattern using only // one loop. function generatePattern(n) { // Iterate for n lines for (let i = 1; i <= n; i++) { console.log("*".repeat(i).padStart(n)); } // Remove multi-line commenting characters below // to get PATTERN WITH CHARACTERS IN LEFT AND // SPACE IN RIGHT /* for (let i = 1; i <= n; i++) { console.log("*".repeat(i).padEnd(n)); } */ } // Driver code const n = 6; generatePattern(n); // This code is contributed by akashish__ Output: * ** *** **** ***********Time complexity: O(n2) for given input nAuxiliary space: O(1) 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 write.geeksforgeeks.org or mail your article to [email protected]. Comment More infoAdvertise with us Next Article Print the pattern by using one loop | Set 2 (Using Continue Statement) K kartik Improve Article Tags : DSA pattern-printing cpp-input-output Practice Tags : pattern-printing Similar Reads Print the pattern by using one loop | Set 2 (Using Continue Statement) Given a number n, print triangular pattern. We are allowed to use only one loop.Example: Input: 7 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * We use single for-loop and in the loop we maintain two variables for line count and current star count. If current star count is less than 5 min read How to print N times without using loops or recursion ? How to print "Hello" N times (where N is user input) without using loop or recursion or goto. Input : N, that represent the number of times you want to print the statement. Output : Statement for N times First, we create a class. After that, we need to initialize the constructor of the class by writ 1 min read Print n to 1 without loop You are given an integer n. Print numbers from n to 1 without the help of loops.Examples:Input: n = 5Output: 5 4 3 2 1Explanation: We have to print numbers from 5 to 1.Input: n = 10Output: 10 9 8 7 6 5 4 3 2 1Explanation: We have to print numbers from 10 to 1.Approach: If we take a look at this prob 3 min read Print n to 1 without loop You are given an integer n. Print numbers from n to 1 without the help of loops.Examples:Input: n = 5Output: 5 4 3 2 1Explanation: We have to print numbers from 5 to 1.Input: n = 10Output: 10 9 8 7 6 5 4 3 2 1Explanation: We have to print numbers from 10 to 1.Approach: If we take a look at this prob 3 min read Program to print a rectangle pattern Given height h and width w, print a rectangular pattern as shown in the example below. Examples: Input : h = 4, w = 5 Output : @@@@@ @ @ @ @ @@@@@ Input : h = 7, w = 9 Output : @@@@@@@@ @ @ @ @ @ @ @ @ @ @ @@@@@@@@ The idea is to run two loops. One for the number of rows to be printed and the other 4 min read Program to print Sine-Wave Pattern Given two integers waveHeight and waveLength. The task is to print a sine wave pattern using the character 0. The sine wave rises and falls vertically across the given height and repeats horizontally for the given length. Examples:Input: waveHeight = 5, waveLength = 10Output: 0 0 0 0 0 0 0 0 0 0 0 0 15+ min read Like