Print the pattern by using one loop | Set 2 (Using Continue Statement) Last Updated : 16 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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 current line count, we print a star and continue. Else we print a new line and increment line count. C++ // C++ program to print a pattern using single // loop and continue statement #include<bits/stdc++.h> using namespace std; // printPattern function to print pattern void printPattern(int n) { // Variable initialization int line_no = 1; // Line count // Loop to print desired pattern int curr_star = 0; for (int line_no = 1; line_no <= n; ) { // If current star count is less than // current line number if (curr_star < line_no) { cout << "* "; curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { cout << "\n"; line_no++; curr_star = 0; } } } // Driver code int main() { printPattern(7); return 0; } Java // Java program to print a pattern using single // loop and continue statement import java.io.*; class GFG { // printPattern function to print pattern static void printPattern(int n) { // Variable initialization // Line count int line_no = 1; // Loop to print desired pattern int curr_star = 0; for ( line_no = 1; line_no <= n;) { // If current star count is less than // current line number if (curr_star < line_no) { System.out.print ( "* "); curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { System.out.println (""); line_no++; curr_star = 0; } } } // Driver code public static void main (String[] args) { printPattern(7); } } // This code is contributed by vt_m Python 3 # Python 3 program to print # a pattern using single # loop and continue statement # printPattern function # to print pattern def printPattern(n): # Variable initialization line_no = 1 # Line count # Loop to print # desired pattern curr_star = 0 line_no = 1 while(line_no <= n ): # If current star count # is less than current # line number if (curr_star < line_no): print("* ", end = "") curr_star += 1 continue # Else time to print # a new line if (curr_star == line_no): print("") line_no += 1 curr_star = 0 # Driver code printPattern(7) # This code is contributed # by Smitha C# // C# program to print a pattern using single // loop and continue statement using System; class GFG { // printPattern function to print pattern static void printPattern(int n) { // Variable initialization // Line count int line_no = 1; // Loop to print desired pattern int curr_star = 0; for ( line_no = 1; line_no <= n;) { // If current star count is less than // current line number if (curr_star < line_no) { Console.Write ( "* "); curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { Console.WriteLine (); line_no++; curr_star = 0; } } } // Driver code public static void Main () { printPattern(7); } } // This code is contributed by vt_m PHP <?php // php program to print a // pattern using single loop // and continue statement // printPattern function // to print pattern function printPattern($n) { // Variable initialization $line_no = 1; // Line count // Loop to print desired pattern $curr_star = 0; for ($line_no = 1; $line_no <= $n;) { // If current star count is less // than current line number if ($curr_star < $line_no) { echo "* "; $curr_star++; continue; } // Else time to print // a new line if ($curr_star == $line_no) { echo "\n"; $line_no++; $curr_star = 0; } } } // Driver code $n=7; printPattern($n); // This code is contributed by mits ?> JavaScript <script> // JavaScript program to print a pattern using single // loop and continue statement // printPattern function to print pattern function printPattern(n) { // Variable initialization var line_no = 1; // Line count // Loop to print desired pattern var curr_star = 0; for (var line_no = 1; line_no <= n; ) { // If current star count is less than // current line number if (curr_star < line_no) { document.write("* "); curr_star++; continue; } // Else time to print a new line if (curr_star == line_no) { document.write("<br>"); line_no++; curr_star = 0; } } } // Driver code printPattern(7); // This code is contributed by rdtank. </script> Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * Time Complexity: O(n2)Auxiliary Space: O(1)Please refer below post for one more approach. Print pattern using only one loop 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 Basic Coding Problems pattern-printing Practice Tags : pattern-printing Similar Reads Print pattern using only one loop | Set 1 (Using setw) 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 ast 4 min read Printing triangle star pattern using a single loop Given a number N, the task is to print the star pattern in single loop. Examples:Â Input: N = 9Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Input: N = 5Output: * * * * * * * * * * * * * * * Please Refer article for printing the pattern in two loop 10 min read Programs to print Triangle and Diamond patterns using recursion This article is aimed at giving a recursive implementation for pattern printing. Pattern 1: Example:Input: 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Implementation: C++ // Program to print the given pattern #include <iostream> using namespace std; void print_asterisk 15+ min read Program to print diamond pattern using numbers and stars Program to print the following pattern of a half diamond for N. Pattern for N = 4Example: Input: N = 5Output: 1 2*3 4*5*6 7*8*9*10 11*12*13*14*15 11*12*13*14*15 7*8*9*10 4*5*6 2*3 1 This program is divided into four parts. C++ // C++ program to print the half diamond // pattern using numbers and sta 5 min read Inner reducing pattern printing Given a number N, print the following pattern. Examples : Input : 4 Output : 4444444 4333334 4322234 4321234 4322234 4333334 4444444 Explanation: (1) Given value of n forms the outer-most rectangular box layer. (2) Value of n reduces by 1 and forms an inner rectangular box layer. (3) The step (2) is 5 min read Like