Program to Print Pyramid Pattern using numbers - GeeksforGeeks

22 Apr, 2021
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
Java
Python3
C#
PHP
Javascript
// C++ Program to print pyramid// pattern using numbers#include <iostream>using namespace std;// Function to print pyramid patternclass 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 codeint main(){    gfg g;    int n = 6;    g.printPattern(n);    return 0;}//this code is contributed by Soumik
// C Program to print pyramid// pattern using numbers#include <stdio.h> // Function to print pyramid patternvoid 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 codeint main(){    int n = 6;    printPattern(n);    return 0;}
// Java Program to print pyramid// pattern using numbersclass GFG{// Function to print pyramid patternstatic 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 codepublic static void main(String []args){    int n = 6;    printPattern(n);}} // This code is contributed by iAyushRaj
# Python3 Program to print pyramid# pattern using numbers # Function to print pyramid patterndef 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 coden = 6printPattern(n)     # This code is contributed# by iAyushRaj
// C# Program to print pyramid// pattern using numbersusing System; class GFG{// Function to print pyramid patternstatic 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 codepublic static void Main(){    int n = 6;    printPattern(n);}} // This code is contributed by iAyushRaj
<?php// PHP Program to print pyramid// pattern using numbers // Function to print pyramid patternfunction 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?>
<script>       // JavaScript Program to print pyramid      // pattern using numbers       // Function to print pyramid pattern       function printPattern(n) {        var t;         // outer loop to print rows        for (var i = 1; i <= n; i++) {          // inner loop to print spaces          for (var j = i; j < n; j++)          document.write("  ");           // calculate initial value          t = i;           // inner loop to print pattern          for (var k = 1; k <= i; k++) {            document.write(t + "   ");            t = t + n - k;          }          document.write("<br>");        }      }       // Driver code      var n = 6;      printPattern(n);           </script>
                     1        
                2        7        
            3        8        12        
        4        9        13        16        
    5        10        14        17        19        
6        11        15        18        20        21
 
iAyushRaj
SoumikMondal
rdtank
pattern-printing
Technical Scripter 2018
C Programs
Technical Scripter
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C Program to read contents of Whole File
Header files in C/C++ and its uses
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
How to Append a Character to a String in C
C program to sort an array in ascending order
time() function in C
C Program to Swap two Numbers
Producer Consumer Problem in C
Program to find Prime Numbers Between given Interval