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++
// 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 = 6printPattern(n) # This code is contributed by Nikita Tiwari. |
c#
// 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.*/ |
PHP
<?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 ?> |
Output:
#
# #
# #
# #
# #
# #
###########
2. Hollow inverted pyramid/triangle pattern
Examples:
Input rows: 6
Output:
###########
# #
# #
# #
# #
#
C++
// 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; } } |
Java
// 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.*/ |
Python
# 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 = 6printPattern(n) # This code is contributed by Nikita Tiwari. |
c#
// 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.*/ |
PHP
<?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 ?> |
Output:
###########
# #
# #
# #
# #
#
3.Hollow diamond pattern
Examples:
Input rows: 6
Output:
#
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#
C++
// 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; } } |
Java
// 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.*/ |
Python
# 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 = 6printPattern(n) # This code is contributed by Nikita Tiwari. |
c#
// 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.*/ |
PHP
<?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 ?> |
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.
Recommended Posts:
- Program to print Inverse Diamond pattern
- Program to print numbers with diamond pattern
- Program to print hollow Triangle pattern
- Program to print the pattern with two hollow Triangles
- Program to print half diamond Number-Star pattern
- Program to print pyramid pattern
- Program to print pyramid pattern
- Program to Print Pyramid Pattern using numbers
- Program to print a inverse pyramid character pattern
- Print the following pyramid pattern
- Program for diamond pattern with different layers
- Print the pyramid pattern with given height and minimum number of stars
- Program to print the diamond shape
- Program to Print Mirrored Hollow Parallelogram
- Program to print the hollow numerical parallelogram
Improved By : Mithun Kumar



