Program to Print Pyramid Pattern using numbers

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


C

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 = 6
printPattern(n)
      
# This code is contributed 
# by iAyushRaj

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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


Output:

                     1        
                2        7        
            3        8        12        
        4        9        13        16        
    5        10        14        17        19        
6        11        15        18        20        21


My Personal Notes arrow_drop_up


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.



Improved By : iAyushRaj, SoumikMondal