Program to print the Zigzag pattern Last Updated : 11 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Given a number N denoting the number of rows. The task is to print the zigzag pattern with N rows as shown in the below examples.Examples: Input : N = 3 Output : 1 3*2 4*5*6 Input : N = 5 Output : 1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 Approach: Use a for loop for printing the number of rows.Use two variables var and var1 for odd and even rows respectively.When the row number is odd, calculate starting point of the row and then print and increment the variable simultaneously.When the row number is even, calculate corresponding starting point and print and decrement the variable simultaneously. Below is the implementation of the above approach: C++ // CPP program to print the given // zigzag pattern #include<iostream> using namespace std; // Function to print the zigzag pattern void printPattern(int n) { int var1, var = 1; for(int i = 1; i <= n; i++) { // for odd rows if(i%2!=0) { // calculate starting value var = var + i - 1; for(int j=1; j<=i; j++) { if(j==1) { cout<<var; } else cout<<"*"<<var; var++; } } else // for even rows { var1 = var + i -1; // calculate starting value for(int j=1; j<=i; j++) { if(j==1) { // print without star cout<<var1; } else { // print with star cout<<"*"<<var1; } var1--; } } cout<<endl; } } // Driver code int main() { int n = 5; printPattern(n); return 0; } Java // Java program to print the given // zigzag pattern class GFG { // Function to print the // zigzag pattern static void printPattern(int n) { int var1, var = 1; for(int i = 1; i <= n; i++) { // for odd rows if(i % 2 != 0) { // calculate starting value var = var + i - 1; for(int j = 1; j <= i; j++) { if(j == 1) { System.out.print(var); } else System.out.print("*" + var); var++; } } else // for even rows { var1 = var + i -1; // calculate starting value for(int j = 1; j <= i; j++) { if(j == 1) { // print without star System.out.print(var1); } else { // print with star System.out.print("*" + var1); } var1--; } } System.out.print("\n"); } } // Driver code public static void main(String [] arg) { int n = 5; printPattern(n); } } // This code is contributed by Smitha Python3 # Python3 program to print the given # zigzag pattern # Function to print the zigzag pattern def printPattern(n): var = 0 var = 1 for i in range(1, n + 1): # for odd rows if(i % 2 != 0): # calculate starting value var = var + i - 1 for j in range(1, i + 1): if(j == 1): print(var, end = "") else: print("*", end = "") print(var, end = "") var += 1 else: # for even rows var1 = var + i -1 # calculate starting value for j in range(1, i + 1): if(j == 1): # prwithout star print(var1, end = "") else: # prwith star print("*", end = "") print(var1, end = "") var1 -= 1 print() # Driver code n = 5 printPattern(n) # This code is contributed by Mohit kumar C# // C# program to print the given // zigzag pattern using System; class GFG { // Function to print the // zigzag pattern static void printPattern(int n) { int var1, var = 1; for(int i = 1; i <= n; i++) { // for odd rows if(i % 2 != 0) { // calculate starting value var = var + i - 1; for(int j = 1; j <= i; j++) { if(j == 1) { Console.Write(var); } else Console.Write("*" + var); var++; } } else // for even rows { var1 = var + i -1; // calculate starting value for(int j = 1; j <= i; j++) { if(j == 1) { // print without star Console.Write(var1); } else { // print with star Console.Write("*" + var1); } var1--; } } Console.Write("\n"); } } // Driver code public static void Main() { int n = 5; printPattern(n); } } // This code is contributed // by Akanksha Rai(Abby_akku) PHP <?php // PHP program to print the given // zigzag pattern // Function to print the zigzag pattern function printPattern($n) { $var1; $var = 1; for($i = 1; $i <= $n; $i++) { // for odd rows if($i % 2 != 0) { // calculate starting value $var = $var + $i - 1; for($j = 1; $j <= $i; $j++) { if($j == 1) { echo $var; } else echo "*" . $var; $var++; } } else // for even rows { // calculate starting value $var1 = $var + $i - 1; for($j = 1; $j <= $i; $j++) { if($j == 1) { // print without star echo $var1; } else { // print with star echo "*" . $var1; } $var1--; } } echo "\n"; } } // Driver code $n = 5; printPattern($n); // This code is contributed by Rajput-Ji ?> JavaScript <script> // JavaScript program to print the given // zigzag pattern // Function to print the zigzag pattern function printPattern(n) { var var1, var2 = 1; for (var i = 1; i <= n; i++) { // for odd rows if (i % 2 != 0) { // calculate starting value var2 = var2 + i - 1; for (var j = 1; j <= i; j++) { if (j == 1) { document.write(var2); } else document.write("*" + var2); var2++; } } // for even rows else { var1 = var2 + i - 1; // calculate starting value for (var j = 1; j <= i; j++) { if (j == 1) { // print without star document.write(var1); } else { // print with star document.write("*" + var1); } var1--; } } document.write("<br>"); } } // Driver code var n = 5; printPattern(n); // This code is contributed by rdtank. </script> Output: 1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 Time Complexity: O(N^2)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to print the Zigzag pattern K Kasthuri Palanichamy Follow Improve Article Tags : Algorithms DSA Technical Scripter 2018 Practice Tags : Algorithms Similar Reads Program to Print the Trapezium Pattern Given 'num' which indicates number of lines.The task is to print a trapezium pattern in num lines.Examples: Input : 4 Output : 1*2*3*4*17*18*19*20 5*6*7*14*15*16 8*9*12*13 10*11 Input : 2 Output : 1*2*5*6 3*4 Algorithm : step 1. To read num which indicates the number of lines. step 2.We are diving t 6 min read Program to print the pattern "GFG" In this article, given the value of n(length of the alphabet) and k(width of the alphabet) we will learn how to print the pattern "GFG" using stars and white-spaces. Examples: INPUT: n=7, k=5 OUTPUT: ***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * ***** INPUT: n=11, k=7 OU 8 min read Program to print the arrow pattern Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C++ // C++ program to print the // arrow pattern #in 10 min read Program to print pattern Given the value of n, print the following pattern.Examples : Input : n = 4 Output : A1 AB12 ABC123 ABCD1234 Input : n = 7 Output : A1 AB12 ABC123 ABCD1234 ABCDE12345 ABCDEF123456 ABCDEFG1234567 Below is the implementation to print the above pattern : C++ // C++ program to print given pattern #includ 5 min read Program to print Spiral Pattern Given a number n as the size of the matrix, the task is to print a spiral pattern in 2D array of size n. Examples: Input: n = 5 Output: 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 Preview of the Spiral Pattern: Approach: Create a 2D array of size nStore the boundary of the arra 10 min read Like