Table of Contents

Writing a program to print a hollow diagram pattern is a typical question you may encounter in a competitive examination or an interview. Implementing this program’s general concept is essential to equip you with logical skills vital in any application development that requires some logical thinking. Besides, the experience will be worthwhile in the development of games.

In the program, we will illustrate the pattern using stars (*) first then, the second example will use the hash (#) for the given number of lines. Also, the user’s row number input indicates the count of lines printed for the case of a single triangular shape of the hollow diamond pattern.

For example, our sample takes eight rows. The total count of lines taken up by the hollow diamond pattern is fifteen, i.e., eight rows for the upper triangular shape and eight rows for the lower triangular shape. Also, note that one row overlaps between the two shapes, thus counting 15 rows in total instead of 16.

## Hollow Diamond Pattern Programs in Python

Below are the examples to illustrate this concept using while and for loops in python.

### Example 1: A program for creating a hollow diamond pattern using stars (*)

""" Python program for creating a hollow diamond pattern using stars (*) """ def drawStarredHollowDiamondPattern(n): """ :param n: is the number of rows for the diamond pattern :return: """ k = 0 ''' drawing the upper triangular shape ''' for j in range(1, n + 1): # first,print the spaces for p in range(1, n - j + 1): print(" ", end="") # secondly,print * while (k != (2 * j - 1)): if (k == 0 or k == 2 * j - 2): print("*", end="") else: print(" ", end="") k = k + 1 k = 0 # finally, proceed to next row print(""), n = n - 1 ''' drawing the lower triangular shape ''' for j in range(n, 0, -1): #first, print the spaces for p in range(0, n - j + 1): print(" ", end="") # secondly, print * k = 0 while (k != (2 * j - 1)): if (k == 0 or k == 2 * j - 2): print("*", end="") else: print(" ", end="") k = k + 1 print(""), ''' Driver code to test the program ''' if __name__=='__main__': number_of_rows = int(input('Input the Number of rows : ')) drawStarredHollowDiamondPattern(number_of_rows)

### Example 2: A program for creating a hollow diamond pattern using a hash (#)

""" Python program for creating a hollow diamond pattern using a hash(#) """ def drawHashedHollowDiamondPattern(n): """ :param n: is the number of rows for the diamond pattern :return: """ k = 0 ''' drawing the upper triangular shape ''' for j in range(1, n + 1): # first,print the spaces for p in range(1, n - j + 1): print(" ", end="") # secondly,print # while (k != (2 * j - 1)): if (k == 0 or k == 2 * j - 2): print("#", end="") else: print(" ", end="") k = k + 1 k = 0 # finally, proceed to next row print(""), n = n - 1 ''' drawing the lower triangular shape ''' for j in range(n, 0, -1): #first, print the spaces for p in range(0, n - j + 1): print(" ", end="") # secondly, print # k = 0 while (k != (2 * j - 1)): if (k == 0 or k == 2 * j - 2): print("#", end="") else: print(" ", end="") k = k + 1 print(""), ''' Driver code to test the program ''' if __name__=='__main__': number_of_rows = int(input('Input the Number of rows : ')) drawHashedHollowDiamondPattern(number_of_rows)