Program to print hollow pyramid and diamond pattern

Prerequisite : Loops, If Else Statement

1. Hollow pyramid/triangle pattern
The pattern is similar to pyramid pattern. The only difference is, we will replace all internal ‘#’ characters by space character and we will 2*N-1 (N = number of rows in pattern) ‘#’ characters in last row.
Examples:

Input rows: 6
Output:
     #
    # #
   #   #
  #     #
 #       #
#         #
###########    

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print a hollow pyramid pattern
#include <iostream>
using namespace std;
void printPattern(int);
int main()
{
    int n = 6;
  
    printPattern(n);
}
void printPattern(int n)
{
    int i, j, k = 0;
    for (i = 1; i <= n; i++) // row=6
    {
        // Print spaces
        for (j = i; j < n; j++) {
            cout << " ";
        }
        // Print #
        while (k != (2 * i - 1)) {
            if (k == 0 || k == 2 * i - 2)
                cout << "#";
            else
                cout << " ";
            k++;
            ;
        }
        k = 0;
        cout << endl; // print next row
    }
    // print last row
    for (i = 0; i < 2 * n - 1; i++) {
        cout << "#";
    }
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program to print a hollow 
// pyramid pattern
class GFG{
      
    public static void main(String args[])
    {
        int n = 6;
      
        printPattern(n);
    }
      
    static void printPattern(int n)
    {
        int i, j, k = 0;
        for (i = 1; i <= n; i++) // row=6
        {
            // Print spaces
            for (j = i; j < n; j++) {
                System.out.print(" ");
            }
            // Print #
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    System.out.print("#");
                else
                    System.out.print(" ");
                k++;
                ;
            }
            k = 0;
              
            // print next row
            System.out.println(); 
        }
        // print last row
        for (i = 0; i < 2 * n - 1; i++) {
            System.out.print("#");
        }
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print a hollow 
# pyramid pattern
  
def printPattern( n) :
    k = 0
    for i in range(1,n+1) : #row 6
      
        # Print spaces
        for j in range(i,n) :
            print(' ', end='')
          
        # Print #
        while (k != (2 * i - 1)) :
            if (k == 0 or k == 2 * i - 2) :
                print('#', end='')
            else :
                print(' ', end ='')
            k = k + 1
        k = 0;
        print ("") # print next row
          
    # print last row
    for i in range(0, 2 * n -1) :
        print ('#', end = '')
  
# Driver code 
n = 6
printPattern(n)
  
# This code is contributed by Nikita Tiwari.

chevron_right


c#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print a hollow 
// pyramid pattern
using System;
class GFG{
      
    public static void Main()
    {
        int n = 6;
      
        printPattern(n);
    }
      
    static void printPattern(int n)
    {
        int i, j, k = 0;
        for (i = 1; i <= n; i++) // row=6
        {
            // Print spaces
            for (j = i; j < n; j++) {
                Console.Write(" ");
            }
            // Print #
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    Console.Write("#");
                else
                    Console.Write(" ");
                k++;
                ;
            }
            k = 0;
              
            // print next row
            Console.WriteLine(); 
        }
        // print last row
        for (i = 0; i < 2 * n - 1; i++) {
            Console.Write("#");
        }
    }
}
  
/*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 a
// hollow pyramid pattern
  
function printPattern($n)
{
    $k = 0;
      
    // row=6
    for ($i = 1; $i <= $n; $i++) 
    {
          
        // Print spaces
        for ($j = $i; $j < $n; $j++) 
        {
            echo " ";
        }
          
        // Print #
        while ($k != (2 * $i - 1)) 
        {
            if ($k == 0 || $k == 2 * 
                             $i - 2)
                echo "#";
            else
                echo " ";
            $k++;
        }
        $k = 0;
          
        // print next row
        echo "\n"
    }
      
    // print last row
    for ($i = 0; $i < 2 * $n - 1; $i++)
    {
        echo "#";
    }
}
  
//Driver Code
$n = 6;
printPattern($n);
  
// This code is contributed by mits 
?>

chevron_right



Output:



     #
    # #
   #   #
  #     #
 #       #
#         #
###########

2. Hollow inverted pyramid/triangle pattern
Examples:

Input rows: 6

Output:
###########
 #       #
  #     #
   #   #
    # #
     #

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print a hollow
// inverted pyramid pattern
  
#include <iostream>
using namespace std;
void printPattern(int);
int main()
{
    int n = 6;
  
    printPattern(n);
}
void printPattern(int n)
{
    int i, j;
    for (i = 1; i <= n; i++) {
        // Print  spaces
        for (j = 1; j < i; j++) {
            cout << " ";
        }
  
        // Print hollow inverted pyramid
        for (j = 1; j <= (n * 2 - (2 * i - 1)); j++) {
  
            if (i == 1 || j == 1 || j == (n * 2 - (2 * i - 1))) {
                cout << "#";
            } else {
                cout << " ";
            }
        }
  
        // next line
        cout << endl;
    }
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print a hollow
// inverted pyramid pattern
class GFG{
  
    public static void main(String args[])
    {
        int n = 6;
      
        printPattern(n);
    }
      
    static void printPattern(int n)
    {
        int i, j;
        for (i = 1; i <= n; i++) {
      
            // Print spaces
            for (j = 1; j < i; j++) {
                System.out.print(" ");
            }
      
            // Print hollow inverted pyramid
            for (j = 1; j <= (n * 2 - (2 * i - 1));
                                        j++) {
      
                if (i == 1 || j == 1 || 
                    j == (n * 2 - (2 * i - 1))) {
                    System.out.print("#");
                }
                else {
                    System.out.print(" ");
                }
            }
      
            // next line
            System.out.println();
        }
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print a hollow
# inverted pyramid pattern
  
def printPattern(n) :
      
    for i in range(1,n+1) :
          
        # Print spaces
        for j in range(1,i) :
            print (" ",end="")
              
        # Print hollow inverted pyramid
        for j in range(1,(n * 2 - (2 * i - 1)) 
                                       +1) :
            if (i == 1 or j == 1 or 
               j == (n * 2 - (2 * i - 1))) :
                print ("#", end="") 
            else :
                print(" ", end="") 
                  
        # next line
        print (""),
  
# Driver code
n = 6
printPattern(n)
  
# This code is contributed by Nikita Tiwari.

chevron_right


c#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print a hollow
// inverted pyramid pattern
using System;
class GFG{
  
    public static void Main()
    {
        int n = 6;
      
        printPattern(n);
    }
      
    static void printPattern(int n)
    {
        int i, j;
        for (i = 1; i <= n; i++) {
      
            // Print spaces
            for (j = 1; j < i; j++) {
             Console.Write(" ");
            }
      
            // Print hollow inverted pyramid
            for (j = 1; j <= (n * 2 - (2 * i - 1));
                                        j++) {
      
                if (i == 1 || j == 1 || 
                    j == (n * 2 - (2 * i - 1))) {
                    Console.Write("#");
                }
                else {
                    Console.Write(" ");
                }
            }
      
            // next line
            Console.WriteLine();
        }
    }
}
  
/*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 a hollow
// inverted pyramid pattern
  
function printPattern($n)
{
    for ($i = 1; $i <= $n; $i++) 
    {
          
        // Print spaces
        for ($j = 1; $j < $i; $j++) 
        {
            echo " ";
        }
  
        // Print hollow inverted pyramid
        for ($j = 1; $j <= ($n
             2 - (2 * $i - 1)); $j++)
        {
  
            if ($i == 1 || $j == 1 || 
                $j == ($n * 2 - (2 * $i - 1)))
            {
                echo "#";
            
            else 
            {
                echo " ";
            }
        }
  
        // next line
        echo "\n";
    }
}
  
    // Driver Code
    $n = 6;
    printPattern($n);
  
// This code is contributed by mits 
?>

chevron_right



Output:

###########
 #       #
  #     #
   #   #
    # #
     #

3.Hollow diamond pattern
Examples:

Input rows: 6
Output:
     #
    # #
   #   #
  #     #
 #       #
#         #
 #         #
  #       #
   #     #
    #   #
     # #
      #
    

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print a hollow
// diamond pattern
#include <iostream>
using namespace std;
void printPattern(int);
int main()
{
    int n = 6;
  
    printPattern(n);
}
void printPattern(int n)
{
    int i, j, space, k = 0;
  
    // Print upper triangle
    for (i = 1; i <= n; i++) {
        // Print spaces
        for (j = 1; j <= n - i; j++) {
            cout << " ";
        }
        // Print #
        while (k != (2 * i - 1)) {
            if (k == 0 or k == 2 * i - 2)
                cout << "#";
            else
                cout << " ";
            k++;
        }
        k = 0;
        // move to next row
        cout << endl;
    }
    n--;
    // Print lower triangle
    for (i = n; i >= 1; i--) {
        // Print spaces
        for (j = 0; j <= n - i; j++) {
            cout << " ";
        }
        // Print #
        k = 0;
        while (k != (2 * i - 1)) {
            if (k == 0 or k == 2 * i - 2)
                cout << "#";
            else
                cout << " ";
            k++;
        }
        cout << endl;
    }
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA program to print a hollow
// diamond pattern
class GFG{
      
    public static void main(String args[])
    {
        int n = 6;
      
        printPattern(n);
    }
      
    static void printPattern(int n)
    {
        int i, j, space, k = 0;
      
        // Print upper triangle
        for (i = 1; i <= n; i++) {
      
            // Print spaces
            for (j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
      
            // Print #
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    System.out.print("#");
                else
                    System.out.print(" ");
                k++;
            }
            k = 0;
      
            // move to next row
            System.out.println();
        }
        n--;
      
        // Print lower triangle
        for (i = n; i >= 1; i--) {
      
            // Print spaces
            for (j = 0; j <= n - i; j++) {
                System.out.print(" ");
            }
      
            // Print #
            k = 0;
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    System.out.print("#");
                else
                    System.out.print(" ");
                k++;
            }
            System.out.println();
        }
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to print a hollow
# diamond pattern
  
def printPattern(n) :
    k = 0;
  
    # Print upper triangle
    for i in range(1,n+1) :
  
        # Print spaces
        for j in range(1,n-i+1) :
            print(" ",end="")
              
        # Print #
        while (k != (2 * i - 1)) :
            if (k == 0 or k == 2 * i - 2) :
                print("#",end="")
            else :
                print(" ",end="")
            k = k + 1
              
        k = 0
  
        # move to next row
        print(""),
      
    n = n - 1
  
    # Print lower triangle
    for i in range (n,0,-1) :
        # Print spaces
        for j in range(0,n-i+1) :
            print(" ",end="")
              
        # Print #
        k = 0
        while (k != (2 * i - 1)) :
            if (k == 0 or k == 2 * i - 2) :
                print("#",end="")
            else :
                print(" ",end="")
            k = k + 1
          
        print(""),
              
# Driver code
n = 6
printPattern(n)
  
# This code is contributed by Nikita Tiwari.

chevron_right


c#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print a hollow
// diamond pattern
using System;
class GFG{
      
    public static void Main()
    {
        int n = 6;
      
        printPattern(n);
    }
      
    static void printPattern(int n)
    {
        int i, j, k = 0;
      
        // Print upper triangle
        for (i = 1; i <= n; i++) {
      
            // Print spaces
            for (j = 1; j <= n - i; j++) {
                Console.Write(" ");
            }
      
            // Print #
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    Console.Write("#");
                else
                Console.Write(" ");
                k++;
            }
            k = 0;
      
            // move to next row
               Console.WriteLine();
        }
        n--;
      
        // Print lower triangle
        for (i = n; i >= 1; i--) {
      
            // Print spaces
            for (j = 0; j <= n - i; j++) {
                Console.Write(" ");
            }
      
            // Print #
            k = 0;
            while (k != (2 * i - 1)) {
                if (k == 0 || k == 2 * i - 2)
                    Console.Write("#");
                else
                    Console.Write(" ");
                k++;
            }
            Console.WriteLine();
        }
    }
}
  
/*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 a 
// hollow diamond pattern
  
function printPattern($n)
{
      
    $k = 0;
  
    // Print upper triangle
    for ($i = 1; $i <= $n; $i++) 
    {
          
        // Print spaces
        for ($j = 1; $j <= $n - $i; $j++) 
        {
            echo " ";
        }
          
        // Print #
        while ($k != (2 * $i - 1)) 
        {
            if ($k == 0 or $k == 2 * $i - 2)
                echo "#";
            else
                echo " ";
            $k++;
        }
        $k = 0;
          
        // move to next row
        echo "\n";
    }
    $n--;
      
    // Print lower triangle
    for ($i = $n; $i >= 1; $i--)
    {
          
        // Print spaces
        for ($j = 0; $j <= $n - $i; $j++) 
        {
            echo " ";
        }
          
        // Print #
        $k = 0;
        while ($k != (2 * $i - 1)) 
        {
            if ($k == 0 or $k == 2 * $i - 2)
                echo "#";
            else
                echo " ";
            $k++;
        }
        echo "\n";
    }
}
  
    // Driver Code
    $n = 6;
    printPattern($n);
  
// This Code is contributed by mits 
?>

chevron_right



Output:

     
     #
    # #
   #   #
  #     #
 #       #
#         #
 #         #
  #       #
   #     #
    #   #
     # #
      #

This article is contributed by Shivani Ghughtyal. 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