Program to print pyramid pattern

Given a positive integer n. Task is to print the pyramid pattern as described in the examples below.

Examples:

Input : 2
Output :
        1
        22
        1

Input : 3
Output :
        1
        22
        333
        22
        1

Input : 5
Output : 
        1
        22
        333
        4444
        55555
        4444
        333
        22
        1

Algorithm:

//Print All Pattern
printPattern(int N)
    // Print upper pattern
    /* 1
       22
       333
       ...
       N...(N)times   */
    for i->1 to N
        for j->1 to i
            print i
        print next line

    // Print lower triangle
    /* N-1....(N-1)times
       ...
       333
       22
       1 */
    for i->N-1 to 0
        for j->i to 0
            print i
        print next line

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the pyramid pattern
#include <bits/stdc++.h>
using namespace std;
  
// Print the pattern upto n
void printPattern(int n)
{
    // Printing upper part
    for (int i = 1; i <= n; i++) 
    {
        for (int j = 1; j <= i; j++) 
            cout << i;        
        cout << "\n";
    }
  
    // printing lower part
    for (int i = n - 1; i > 0; i--) 
    {
       for (int j = i; j > 0; j--) 
            cout << i;        
        cout << "\n";
    }
}
  
// Driver Code
int main()
{
    int n = 8;
    printPattern(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print the pyramid pattern
  
class GFG {
      
// Print the pattern upto n
static void printPattern(int n) 
{
    // Printing upper part
    for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++)
        System.out.print(i);
    System.out.print("\n");
    }
  
    // printing lower part
    for (int i = n - 1; i > 0; i--) {
    for (int j = i; j > 0; j--)
        System.out.print(i);
    System.out.print("\n");
    }
}
  
// Driver code
public static void main(String arg[]) 
{
    int n = 8;
    printPattern(n);
}
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print
# the pyramid pattern
  
# Print the pattern upto n
def printPattern(n):
  
    # Printing upper part
    for i in range(n+1): 
      
        for j in range(1,i+1):
            print(i,end="")        
        print("")
      
    # printing lower part
    for i in range(n - 1,0,-1): 
      
        for j in range(i,0,-1): 
            print(i,end="")        
        print("")
  
# driver code
n = 8
printPattern(n)
  
# This code is contributed
# by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the pyramid pattern
using System;
class GFG {
      
// Print the pattern upto n
static void printPattern(int n) 
{
    // Printing upper part
    for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++)
        Console.Write(i);
        Console.WriteLine();
    }
  
    // printing lower part
    for (int i = n - 1; i > 0; i--) {
    for (int j = i; j > 0; j--)
        Console.Write(i);
        Console.WriteLine();
    }
}
  
// Driver code
public static void Main() 
{
    int n = 8;
    printPattern(n);
}
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to print the
// pyramid pattern
  
// Print the pattern upto n
function printPattern($n)
{
    // Printing upper part
    for ($i = 1; $i <= $n; $i++) 
    {
        for ($j = 1; $j <= $i; $j++) 
            echo $i;     
        echo "\n";
    }
  
    // printing lower part
    for ($i = $n - 1; $i > 0; $i--) 
    {
    for ($j = $i; $j > 0; $j--) 
            echo $i;     
        echo "\n";
    }
}
  
    // Driver Code
    $n = 8;
    printPattern($n);
  
// This code is contributed by mits    
?>

chevron_right



Output:

1
22
333
4444
55555
666666
7777777
88888888
7777777
666666
55555
4444
333
22
1

This article is contributed by Sahil Rajput. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Mithun Kumar