Mastering Hollow Patterns: A Comprehensive Guide with Code Examples
Tags: c, coding, tutorial, beginners
Published: 2024-05-28 07:07:39

Welcome to our comprehensive guide on creating various hollow patterns using loops in C programming! In this tutorial, we'll walk through step-by-step instructions on how to draw 18 different hollow patterns. These patterns range from basic shapes like squares and triangles to more complex forms like diamonds, hexagons, and pentagons. Each pattern is created using nested loops, making it an excellent exercise for beginners to practice control structures in C. Let's dive in!

You can find all the code in our [GitHub repository](https://github.com/jithesh-poojari/c-programs/blob/main/hollow-patterns.c).

### Table of Contents
1. [Introduction to Nested Loops](#introduction-to-nested-loops)
2. [Hollow Square](#hollow-square)
3. [Hollow Right Triangle](#hollow-right-triangle)
4. [Hollow Inverted Right Triangle](#hollow-inverted-right-triangle)
5. [Hollow Right Aligned Triangle](#hollow-right-aligned-triangle)
6. [Hollow Right Aligned Inverted Triangle](#hollow-right-aligned-inverted-triangle)
7. [Hollow Right Pascal Triangle](#hollow-right-pascal-triangle)
8. [Hollow Left Pascal Triangle](#hollow-left-pascal-triangle)
9. [Hollow Equilateral Triangle](#hollow-equilateral-triangle)
10. [Hollow Inverted Equilateral Triangle](#hollow-inverted-equilateral-triangle)
11. [Hollow Pyramid](#hollow-pyramid)
12. [Hollow Inverted Pyramid](#hollow-inverted-pyramid)
13. [Hollow Diamond](#hollow-diamond)
14. [Hollow Hourglass](#hollow-hourglass)
15. [Hollow Rhombus](#hollow-rhombus)
16. [Hollow Parallelogram](#hollow-parallelogram)
17. [Hollow Hexagon](#hollow-hexagon)
18. [Hollow Pentagon](#hollow-pentagon)
19. [Hollow Inverted Pentagon](#hollow-inverted-pentagon)
20. [Conclusion](#conclusion)

### Introduction to Nested Loops

Before we start with the patterns, it’s essential to understand the concept of nested loops. A nested loop is a loop inside another loop. This structure is particularly useful for handling multi-dimensional arrays and for generating patterns. In C, a typical nested loop structure looks like this:

```c
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        // Code to execute
    }
}
```

### Hollow Square

**Explanation:**
- The hollow square pattern consists of `n` rows and `n` columns.
- Characters are printed only at the borders (first row, last row, first column, and last column).

```c
int n = 5; // size of the square
char ch = '*';

printf("1. Hollow Square:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
*  *  *  *  *  
*           *  
*           *  
*           *  
*  *  *  *  *  
```

### Hollow Right Triangle

**Explanation:**
- The hollow right triangle pattern starts with one character in the first row and increases by one character in each subsequent row.
- Characters are printed only at the borders (first row, last row, and the diagonal).

```c
printf("2. Hollow Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        if (i == n - 1 || j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
*  
*  *  
*     *  
*        *  
*  *  *  *  *  
```

### Hollow Inverted Right Triangle

**Explanation:**
- The hollow inverted right triangle pattern starts with `n` characters in the first row and decreases by one character in each subsequent row.
- Characters are printed only at the borders (first row, last row, and the diagonal).

```c
printf("3. Hollow Inverted Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n; j > i; j--) {
        if (i == 0 || j == n || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
*  *  *  *  *  
*        *  
*     *  
*  *  
*  
```

### Hollow Right Aligned Triangle

**Explanation:**
- The hollow right aligned triangle pattern is similar to the hollow right triangle, but the triangle is right-aligned.
- Characters are printed only at the borders (first row, last row, and the diagonal).

```c
printf("4. Hollow Right Aligned Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < i + 1; j++) {
        if (i == n - 1 || j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
            *  
         *  *  
      *     *  
   *        *  
*  *  *  *  *  
```

### Hollow Right Aligned Inverted Triangle

**Explanation:**
- The hollow right aligned inverted triangle pattern is the opposite of the hollow right aligned triangle.
- It starts with `n` characters in the first row and decreases by one character in each subsequent row, but the triangle is right-aligned.

```c
printf("5. Hollow Right Aligned Inverted Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 1; j < i + 1; j++) {
        printf("   ");
    }
    for (int j = n; j > i; j--) {
        if (i == 0 || j == n || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
*  *  *  *  *  
   *        *  
      *     *  
         *  *  
            *  
```

### Hollow Right Pascal Triangle

**Explanation:**
- The hollow right Pascal triangle pattern combines the right triangle and the inverted right triangle to form a Pascal-like triangle.
- Characters are printed only at the borders (first row, last row, and the diagonals).

```c
printf("6. Hollow Right Pascal Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
for (int i = 0; i < n; i++) {
    for (int j = n; j > i + 1; j--) {
        if (j == n || j == i + 2) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
*  
*  *  
*     *  
*        *  
*           *  
*        *  
*     *  
*  *  
*  
```

### Hollow Left Pascal Triangle

**Explanation:**
- The hollow left Pascal triangle pattern is similar to the hollow right Pascal triangle, but it is left-aligned.
- Characters are printed only at the borders (first row, last row, and the diagonals).

```c
printf("7. Hollow Left Pascal Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}

for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        printf("   ");
    }
    for (int j = n - 1; j > i; j--) {
        if (j == n

 - 1 || j == i + 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
            *  
         *  *  
      *     *  
   *        *  
*           *  
   *        *  
      *     *  
         *  *  
            *  
```

### Hollow Equilateral Triangle

**Explanation:**
- The hollow equilateral triangle pattern is symmetrical and centered.
- Characters are printed only at the borders (first row, last row, and the diagonals).

```c
printf("8. Hollow Equilateral Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < 2 * i + 1; j++) {
        if (j == 0 || j == 2 * i || i == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
            *  
         *     *  
      *           *  
   *                 *  
*  *  *  *  *  *  *  *  * 
```

### Hollow Inverted Equilateral Triangle

**Explanation:**
- The hollow inverted equilateral triangle pattern is the opposite of the hollow equilateral triangle.
- Characters are printed only at the borders (first row, last row, and the diagonals).

```c
printf("9. Hollow Inverted Equilateral Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
        printf("   ");
    }
    for (int j = 2 * n - 1; j > 2 * i; j--) {
        if (j == 2 * n - 1 || j == 2 * i + 1 || i == 0) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
*  *  *  *  *  *  *  *  *  
   *                 *  
      *           *  
         *     *  
            *  
```

### Hollow Pyramid

**Explanation:**
- The hollow pyramid pattern is centered and symmetrical.
- Characters are printed only at the borders (first row, last row, and the diagonals).

```c
printf("10. Hollow Pyramid:\n");
for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
       printf(" ");
    }
    for (j = 0; j < (2 * i + 1); j++) {
        if (i == n - 1 || j == 0 || j == i * 2 ) {
            printf("%c", ch);
        } else {
            printf(" ");
        }
    }
    printf("\n"); 
}
```

**Output:**
```
    *
   * *
  *   *
 *     *
********* 
```

### Hollow Inverted Pyramid

**Explanation:**
- The hollow inverted pyramid pattern is the opposite of the hollow pyramid.
- Characters are printed only at the borders (first row, last row, and the diagonals).

```c
printf("11. Hollow Inverted Pyramid:\n");
for (i = n; i > 0; i--) {
    for (j = n - i; j > 0; j--) {
        printf(" ");
    }
    for (j = 0; j < (2 * i - 1); j++) {
        if (j == 0 || i == n  || j == (i-1) * 2 ) {
            printf("%c", ch);
        } else {
            printf(" ");
        }
    }
  printf("\n");
}
```

**Output:**
```
*********
 *     *
  *   *
   * *
    * 
```

### Hollow Diamond

**Explanation:**
- The hollow diamond pattern is symmetrical and centered.
- It consists of a hollow upper and lower triangle.

```c
printf("12. Hollow Diamond:\n");
for (i = 0; i < n; i++) {
    for (j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (j = 0; j < i + 1; j++) {
        if (j == 0 || j == i) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = 0; i < n; i++) {
    for (j = 0; j < i + 1; j++) {
        printf(" ");
    }
    for (j = n - 1; j > i; j--) {
        if (j == n - 1 || j == i + 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
```

**Output:**
```
    * 
   * * 
  *   * 
 *     * 
*       * 
 *     * 
  *   * 
   * * 
    * 
```

### Hollow Hourglass

**Explanation:**
- The hollow hourglass pattern is symmetrical and centered.
- It consists of a hollow upper and lower inverted triangle.

```c
printf("13. Hollow Hourglass:\n");
for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
        printf(" ");
    }
    for (j = 0; j < (n - i) ; j++) {
        if (j == 0 || i == 0  || j ==  n - i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = 1; i < n; i++) {
    for (j = n - 1; j > i; j--) {
       printf(" ");
    }
    for (j = 0; j < (i + 1); j++) {
       if (i == n - 1 || j == 0 || j == i) {
           printf("%c ", ch);
       } else {
           printf("  ");
       }
     }
     printf("\n");
}
```

**Output:**
```
* * * * * 
 *     * 
  *   * 
   * * 
    * 
   * * 
  *   * 
 *     * 
* * * * * 
```

### Hollow Rhombus

**Explanation:**
- The hollow rhombus pattern is symmetrical and centered.
- Characters are printed only at the borders.

```c
printf("14. Hollow Rhombus:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf("   ");
    }
    for (int j = 0; j < n; j++) {
        if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
            printf("%c  ", ch);
        } else {
            printf("   ");
        }
    }
    printf("\n");
}
```

**Output:**
```
            *  *  *  *  *  
         *           *  
      *           *  
   *           *  
*  *  *  *  *  
```

### Hollow Parallelogram

**Explanation:**
- The hollow parallelogram pattern is symmetrical and slanted to one side.
- Characters are printed only at the borders.

```c
printf("15. Hollow Parallelogram:\n");
for (i = 0; i < n; i++) {
    for (j = 0; j < i; j++) {
        printf(" ");
    }
    for (j = 0; j < n * 2; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n * 2 - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
```

**Output:**
```
* * * * * * * * * * 
 *                 * 
  *                 * 
   *                 * 
    * * * * * * * * * *  
```

### Hollow Hexagon

**Explanation:**
- The hollow hexagon pattern consists of a combination of upper and lower triangles and a middle section.
- Characters are printed only at the borders.

```c
printf("16. Hollow Hexagon:\n");
for (i = 0; i < n / 2; i++) {
    for (j = n / 2 - i; j > 0; j--) {
        printf(" ");
    }
    for (j = 0; j < n + 1 * i; j++) {
        if ( i == 0 || j == 0 || j == n * i) {
           printf("%c ", ch);
        } else {
           printf("  ");
        }
    }
    printf("\n");
}
for (i = n / 2; i >= 0; i--) {
    for (j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (j = 0; j < n + i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
```

**Output:**
```
  * * * * * 
 *         * 
*           * 
 *         * 
  * * * * * 
```

### Hollow Pentagon

**Explanation:**
- The hollow pentagon pattern consists of an upper triangle and a lower rectangle.
- Characters are printed only at the borders.

```c
printf("17. Hollow Pentagon:\n");
for (i = 0; i < n+1; i++) {
    for (j = n ; j > i; j--) {
        printf(" ");
    }
    for (j = 0; j < (i + 1); j++) {
        if ( j == 0 || i == 0 || j == i ) {
            printf(" %c", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (i = n / 2; i >= 0; i--) {
    for (j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (j = 0; j < n +  i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
```

**Output:**
```
      *
     * *
    *   *
   *     *
  *       *
 *         *
*           * 
 *         * 
  * * * * * 
```

### Hollow Inverted Pentagon

**Explanation:**
- The hollow inverted pentagon pattern consists of an upper inverted triangle and a lower inverted rectangle.
- Characters are printed only at the borders.

```c
printf("18. Hollow Inverted Pentagon:\n");
for (int i = 0; i <= n / 2; i++) {
    for (int j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (int j = 0; j < n + i; j++) {
        if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
for (int i = n + 1; i > 0; i--) {
    for (int j = n + 2; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j < i; j++) {
        if ( j == 0 || j == i - 1) {
            printf("%c ", ch);
        } else {
            printf("  ");
        }
    }
    printf("\n");
}
```

**Output:**
```
  * * * * * 
 *         * 
*           * 
 *         * 
  *       * 
   *     * 
    *   * 
     * * 
      *  
```

### Conclusion

In conclusion, we have explored a variety of patterns using loops and conditional statements in C, each producing different geometric shapes and designs. These patterns include solid and hollow variants of squares, triangles, pyramids, diamonds, hourglasses, rhombuses, parallelograms, hexagons, and pentagons. Understanding and implementing these patterns helps to strengthen programming logic, loop constructs, and conditionals, which are fundamental concepts in computer science.

By practicing these patterns, you can enhance your problem-solving skills and improve your ability to visualize and implement complex patterns in code. These exercises also provide a solid foundation for more advanced programming tasks and algorithms.