Skip to content
Related Articles

Related Articles

Improve Article
Program to print hollow pyramid and diamond pattern
  • Difficulty Level : Medium
  • Last Updated : 07 Apr, 2021

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




// 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 << "#";
    }
}

Java




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

Python




# 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.

c#] 


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


Javascript




<script>
      // JavScript program to print a hollow pyramid pattern
      var n = 6;
      printPattern(n);
      function printPattern(n)
      {
        var i, j, k = 0;
        for (i = 1; i <= n; i++)// row=6
        {
          // Print spaces
          for (j = i; j < n; j++)
          {
            document.write("  ");
          }
           
          // Print #
          while (k != 2 * i - 1)
          {
            if (k == 0 || k == 2 * i - 2)
                document.write("#");
            else
                document.write("  ");
            k++;
          }
          k = 0;
          document.write("<br>"); // print next row
        }
         
        // print last row
        for (i = 0; i < 2 * n - 1; i++)
        {
          document.write("#");
        }
      }
       
      // This code is contributed by rdtank.
    </script>

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.
 

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



My Personal Notes arrow_drop_up
Recommended Articles
Page :