DEV Community

Jithesh
Jithesh

Posted on

Mastering Filled Patterns: A Comprehensive Guide with Code Examples

Welcome to our comprehensive guide on creating various filled patterns using loops in C programming! In this tutorial, we'll walk through step-by-step instructions on how to draw 18 different filled 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.

Table of Contents

  1. Introduction to Nested Loops
  2. Filled Square
  3. Filled Right Triangle
  4. Filled Inverted Right Triangle
  5. Filled Right Aligned Triangle
  6. Filled Right Aligned Inverted Triangle
  7. Filled Right Pascal Triangle
  8. Filled Left Pascal Triangle
  9. Filled Equilateral Triangle
  10. Filled Inverted Equilateral Triangle
  11. Filled Pyramid
  12. Filled Inverted Pyramid
  13. Filled Diamond
  14. Filled Hourglass
  15. Filled Rhombus
  16. Filled Parallelogram
  17. Filled Hexagon
  18. Filled Pentagon
  19. Filled Inverted Pentagon
  20. 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:

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        // Code to execute
    }
}
Enter fullscreen mode Exit fullscreen mode

Filled Square

Explanation:

  • The filled square pattern is one of the simplest patterns to create.
  • It consists of n rows and n columns, where each cell contains the same character.
  • We use two nested loops to iterate over each row and column, printing the character in each cell.
int n = 5; // size of the square
char ch = '*';

printf("1. Filled Square:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        printf("%c  ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

*  *  *  *  *  
*  *  *  *  *  
*  *  *  *  *  
*  *  *  *  *  
*  *  *  *  *  
Enter fullscreen mode Exit fullscreen mode

Filled Right Triangle

Explanation:

  • The filled right triangle pattern starts with one character in the first row and increases by one character in each subsequent row.
  • This pattern is achieved by using two nested loops. The outer loop controls the number of rows, and the inner loop controls the number of characters printed in each row.
printf("2. Filled Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
        printf("%c  ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

*  
*  *  
*  *  *  
*  *  *  *  
*  *  *  *  *   
Enter fullscreen mode Exit fullscreen mode

Filled Inverted Right Triangle

Explanation:

  • The filled inverted right triangle pattern is the opposite of the filled right triangle.
  • It starts with n characters in the first row and decreases by one character in each subsequent row.
  • Similar to the filled right triangle, this pattern is created using two nested loops.
printf("3. Filled Inverted Right Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n; j > i; j--) {
        printf("%c  ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

*  *  *  *  *  
*  *  *  *  
*  *  *  
*  *  
*  
Enter fullscreen mode Exit fullscreen mode

Filled Right Aligned Triangle

Explanation:

  • The filled right aligned triangle pattern is similar to the filled right triangle, but the triangle is right-aligned.
  • This pattern is achieved by adding spaces before each row, creating a right-aligned appearance.
printf("4. Filled 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; j++) {
        printf("%c  ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

            *  
         *  *  
      *  *  *  
   *  *  *  *  
*  *  *  *  * 
Enter fullscreen mode Exit fullscreen mode

Filled Right Aligned Inverted Triangle

Explanation:

  • The filled right aligned inverted triangle pattern is the opposite of the filled right aligned triangle.
  • It starts with one character in the first row and increases by one character in each subsequent row, but the triangle is right-aligned.
printf("5. Filled Right Aligned Inverted Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 1; j <= i; j++) {
        printf("   ");
    }
    for (int j = n; j > i; j--) {
        printf("%c  ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

*  *  *  *  *  
   *  *  *  *  
      *  *  *  
         *  *  
            *  
Enter fullscreen mode Exit fullscreen mode

Filled Right Pascal Triangle

Explanation:

  • The filled right Pascal triangle pattern combines the right triangle and the inverted right triangle to form a Pascal-like triangle.
  • The first half of the pattern is similar to the filled right triangle, and the second half is similar to the filled inverted right triangle.
printf("6. Filled Right Pascal Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i + 1; j++) {
        printf("%c  ", ch);
    }
    printf("\n");
}
for (int i = 0; i < n; i++) {
    for (int j = n; j > i + 1; j--) {
        printf("%c  ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

*  
*  *  
*  *  *  
*  *  *  *  
*  *  *  *  *  
*  *  *  *  
*  *  *  
*  *  
*    
Enter fullscreen mode Exit fullscreen mode

Filled Left Pascal Triangle

Explanation:

  • The filled left Pascal triangle pattern is similar to the filled right Pascal triangle, but it is left-aligned.
  • The first half of the pattern is similar to the filled right aligned triangle, and the second half is similar to the filled right aligned inverted triangle.
printf("7. Filled 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++) {
        printf("%c  ", ch);
    }
    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--) {
        printf("%c  ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

            *  
         *  *  
      *  *  *  
   *  *  *  *  
*  *  *  *  *  
   *  *  *  *  
      *  *  *  
         *  *  
            *   
Enter fullscreen mode Exit fullscreen mode

Filled Equilateral Triangle

Explanation:

  • The filled equilateral triangle pattern has a symmetrical shape with each row centered.
  • To achieve this, we print spaces before each row to center the characters.
printf("8. Filled Equilateral Triangle:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j <= i; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

    * 
   * * 
  * * * 
 * * * * 
* * * * *  
Enter fullscreen mode Exit fullscreen mode

Filled Inverted Equilateral Triangle

Explanation:

  • The filled inverted equilateral triangle pattern is the inverted version of the filled equilateral triangle.
  • It starts with n characters at the base and decreases by one character per row, centered.
printf("9. Filled Inverted Equilateral Triangle:\n");
for (int i = n - 1; i >= 0; i--) {
    for (int j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j <= i; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

* * * * * 
 * * * * 
  * * * 
   * * 
    *   
Enter fullscreen mode Exit fullscreen mode

Filled Pyramid

Explanation:

  • The filled pyramid pattern starts with one character at the top and increases by two characters per row, forming a symmetrical pyramid.
  • We use spaces to center each row.
printf("10. Filled Pyramid:\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++) {
        printf("%c", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

    *
   ***
  *****
 *******
*********  
Enter fullscreen mode Exit fullscreen mode

Filled Inverted Pyramid

Explanation:

  • The filled inverted pyramid pattern is the opposite of the filled pyramid.
  • It starts with 2 * n - 1 characters at the top and decreases by two characters per row, centered.
printf("11. Filled Inverted Pyramid:\n");
for (int i = n; i > 0; i--) {
    for (int j = n - i; j > 0; j--) {
        printf(" ");
    }
    for (int j = 0; j < (2 * i - 1); j++) {
        printf("%c", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

*********
 *******
  *****
   ***
    *  
Enter fullscreen mode Exit fullscreen mode

Filled Diamond

Explanation:

  • The filled diamond pattern is formed by combining the filled equilateral triangle and the filled inverted equilateral triangle.
  • It creates a symmetrical diamond shape.
printf("12. Filled Diamond:\n");
for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j <= i; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j <= i; j++) {
        printf(" ");
    }
    for (int j = n - 1; j > i; j--) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    *  
Enter fullscreen mode Exit fullscreen mode

Filled Hourglass

Explanation:

  • The filled hourglass pattern combines an inverted equilateral triangle and an equilateral triangle, forming an hourglass shape.
  • Each row is centered by adding spaces.
printf("13. Filled Hourglass:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
        printf(" ");
    }
    for (int j = 0; j < (n - i); j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
for (int i = 1; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j <= i; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

* * * * * 
 * * * * 
  * * * 
   * * 
    * 
   * * 
  * * * 
 * * * * 
* * * * *   
Enter fullscreen mode Exit fullscreen mode

Filled Rhombus

Explanation:

  • The filled rhombus pattern consists of rows where each row is shifted to the right by spaces.
  • This creates a diamond-like shape with equal length sides.
printf("14. Filled Rhombus:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n - i - 1; j++) {
        printf(" ");
    }
    for (int j = 0; j < n; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

    * * * * * 
   * * * * * 
  * * * * * 
 * * * * * 
* * * * *  
Enter fullscreen mode Exit fullscreen mode

Filled Parallelogram

Explanation:

  • The filled parallelogram pattern is created by shifting each row to the right.
  • It looks like a rectangle leaning to one side.
printf("15. Filled Parallelogram:\n");
for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
        printf(" ");
    }
    for (int j = 0; j < n * 2; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

* * * * * * * * * * 
 * * * * * * * * * * 
  * * * * * * * * * * 
   * * * * * * * * * * 
    * * * * * * * * * *  
Enter fullscreen mode Exit fullscreen mode

Filled Hexagon

Explanation:

  • The filled hexagon pattern has a wider middle section, with each row increasing and then decreasing in width.
  • This creates a hexagonal shape.
printf("16. Filled Hexagon:\n");
for (int i = 0; i < n / 2; i++) {
    for (int j = n / 2 - i; j > 0; j--) {
        printf(" ");
    }
    for (int j = 0; j < n + 1 * i; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
for (int i = n / 2; i >= 0; i--) {
    for (int j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (int j = 0; j < n + i; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

  * * * * * 
 * * * * * * 
* * * * * * * 
 * * * * * * 
  * * * * *  
Enter fullscreen mode Exit fullscreen mode

Filled Pentagon

Explanation:

  • The filled pentagon pattern starts with one character at the top and increases, forming a wider base.
  • This creates a pentagon-like shape.
printf("17. Filled Pentagon:\n");
for (int i = 0; i < n + 1; i++) {
    for (int j = n; j > i; j--) {
        printf(" ");
    }
    for (int j = 0; j < (i + 1); j++) {
        printf(" %c", ch);
    }
    printf("\n");
}
for (int i = n / 2; i >= 0; i--) {
    for (int j = 0; j < n / 2 - i; j++) {
        printf(" ");
    }
    for (int j = 0; j < n + i; j++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

      *
     * *
    * * *
   * * * *
  * * * * *
 * * * * * *
* * * * * * * 
 * * * * * * 
  * * * * *  
Enter fullscreen mode Exit fullscreen mode

Filled Inverted Pentagon

Explanation:

  • The filled inverted pentagon pattern is the inverted version of the filled pentagon.
  • It starts with the wider base and decreases, forming an inverted pentagon shape.
printf("18. Filled 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++) {
        printf("%c ", ch);
    }
    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++) {
        printf("%c ", ch);
    }
    printf("\n");
}
Enter fullscreen mode Exit fullscreen mode

Output:

  * * * * * 
 * * * * * * 
* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      *   
Enter fullscreen mode Exit fullscreen mode

Conclusion

Learning to create these filled patterns in C is an excellent way to practice using nested loops and enhance your understanding of how loops work. By experimenting with different values and shapes, you can deepen your understanding of control structures in C and develop a keen eye for detail and logic. Whether you're a beginner or looking to brush up on your skills, these patterns provide a solid foundation for mastering loops in C programming.

We hope this guide has been helpful and encourages you to explore more complex patterns and designs. Happy coding!

For more tutorials and coding tips, be sure to subscribe to our blog and follow us on social media!

Top comments (0)