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 : 2
Output : 1
2 3
Input : 5
Output : 1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Approach:
1. Use a for loop for printing the number of rows.
2. Use two for loops for printing space and pattern.
3. Calculate starting value for each row with variable t.
Below is the implementation of above approach:
C++
// C++ Program to print pyramid // pattern using numbers #include <iostream> using namespace std; // Function to print pyramid pattern class gfg { public : void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) printf("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { printf("%d\t\t", t); t = t + n - k; } printf("\n"); } } }; // Driver code int main() { gfg g; int n = 6; g.printPattern(n); return 0; } //this code is contributed by Soumik |
chevron_right
filter_none
C
// C Program to print pyramid // pattern using numbers #include <stdio.h> // Function to print pyramid pattern void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) printf("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { printf("%d\t\t", t); t = t + n - k; } printf("\n"); } } // Driver code int main() { int n = 6; printPattern(n); return 0; } |
chevron_right
filter_none
Java
// Java Program to print pyramid // pattern using numbers class GFG { // Function to print pyramid pattern static void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) System.out.print("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { System.out.print(t + "\t\t"); t = t + n - k; } System.out.println(); } } // Driver code public static void main(String []args) { int n = 6; printPattern(n); } } // This code is contributed by iAyushRaj |
chevron_right
filter_none
Python3
# Python3 Program to print pyramid # pattern using numbers # Function to print pyramid pattern def printPattern(n): # outer loop to print rows for i in range(1, n + 1): # inner loop to print spaces for j in range(i, n): print("\t", end = "") # calculate initial value t = i # inner loop to print pattern for k in range (1, i + 1): print(t, "\t", "\t", end = "") t = t + n - k print() # Driver code n = 6printPattern(n) # This code is contributed # by iAyushRaj |
chevron_right
filter_none
C#
// C# Program to print pyramid // pattern using numbers using System; class GFG { // Function to print pyramid pattern static void printPattern(int n) { int t; // outer loop to print rows for (int i = 1; i <= n; i++) { // inner loop to print spaces for (int j = i; j < n; j++) Console.Write("\t"); // calculate initial value t = i; // inner loop to print pattern for (int k = 1; k <= i; k++) { Console.Write(t + "\t\t"); t = t + n - k; } Console.WriteLine(); } } // Driver code public static void Main() { int n = 6; printPattern(n); } } // This code is contributed by iAyushRaj |
chevron_right
filter_none
PHP
<?php // PHP Program to print pyramid // pattern using numbers // Function to print pyramid pattern function printPattern($n) { // outer loop to print rows for ($i = 1; $i <= $n; $i++) { // inner loop to print spaces for ($j = $i; $j < $n; $j++) echo "\t"; // calculate initial value $t = $i; // inner loop to print pattern for ($k = 1; $k <= $i; $k++) { echo "$t \t\t"; $t = $t + $n - $k; } echo "\n"; } } // Driver code $n = 6; printPattern($n); // This code is contributed // by iAyushRaj ?> |
chevron_right
filter_none
Output:
1
2 7
3 8 12
4 9 13 16
5 10 14 17 19
6 11 15 18 20 21
Recommended Posts:
- Program to print pyramid pattern
- Program to print pyramid pattern
- Program to print hollow pyramid and diamond pattern
- Program to print a inverse pyramid character pattern
- Print the following pyramid pattern
- Print the pyramid pattern with given height and minimum number of stars
- Program to print a pattern of numbers
- Program to print numbers with diamond pattern
- Program to print Even Odd Number Pyramid
- Program to print the given Z Pattern
- Program to print the given H Pattern
- Program to print the pattern 'G'
- Program to print the pattern ‘D’
- Program to print the pattern "GFG"
- Program to print the Cot Bed Pattern
- Program to print pattern
- Print numbers in matrix diagonal pattern
- Program to print the Zigzag pattern
- Program to print a rectangle pattern
- Program to print Step Pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

