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.
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; iFilled Square
Explanation:
int n = 5; // size of the square char ch = '*'; printf("1. Filled Square:\n"); for (int i = 0; iOutput:
* * * * * * * * * * * * * * * * * * * * * * * * *Filled Right Triangle
Explanation:
printf("2. Filled Right Triangle:\n"); for (int i = 0; iOutput:
* * * * * * * * * * * * * * *Filled Inverted Right Triangle
Explanation:
printf("3. Filled Inverted Right Triangle:\n"); for (int i = 0; i i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("4. Filled Right Aligned Triangle:\n"); for (int i = 0; i i; j--) { printf(" "); } for (int j = 0; jOutput:
* * * * * * * * * * * * * * *Filled Right Aligned Inverted Triangle
Explanation:
printf("5. Filled Right Aligned Inverted Triangle:\n"); for (int i = 0; i i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("6. Filled Right Pascal Triangle:\n"); for (int i = 0; i i 1; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("7. Filled Left Pascal Triangle:\n"); for (int i = 0; i i; j--) { printf(" "); } for (int j = 0; j i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("8. Filled Equilateral Triangle:\n"); for (int i = 0; i i; j--) { printf(" "); } for (int j = 0; jOutput:
* * * * * * * * * * * * * * *Filled Inverted Equilateral Triangle
Explanation:
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; jOutput:
* * * * * * * * * * * * * * *Filled Pyramid
Explanation:
printf("10. Filled Pyramid:\n"); for (int i = 0; i i; j--) { printf(" "); } for (int j = 0; jOutput:
* *** ***** ******* *********Filled Inverted Pyramid
Explanation:
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; jOutput:
********* ******* ***** *** *Filled Diamond
Explanation:
printf("12. Filled Diamond:\n"); for (int i = 0; i i; j--) { printf(" "); } for (int j = 0; j i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("13. Filled Hourglass:\n"); for (int i = 0; i i; j--) { printf(" "); } for (int j = 0; jOutput:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *Filled Rhombus
Explanation:
printf("14. Filled Rhombus:\n"); for (int i = 0; iOutput:
* * * * * * * * * * * * * * * * * * * * * * * * *Filled Parallelogram
Explanation:
printf("15. Filled Parallelogram:\n"); for (int i = 0; iOutput:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Filled Hexagon
Explanation:
printf("16. Filled Hexagon:\n"); for (int i = 0; i 0; j--) { printf(" "); } for (int j = 0; j = 0; i--) { for (int j = 0; jOutput:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *Filled Pentagon
Explanation:
printf("17. Filled Pentagon:\n"); for (int i = 0; i i; j--) { printf(" "); } for (int j = 0; j = 0; i--) { for (int j = 0; jOutput:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Filled Inverted Pentagon
Explanation:
printf("18. Filled Inverted Pentagon:\n"); for (int i = 0; i 0; i--) { for (int j = n 2; j > i; j--) { printf(" "); } for (int j = 0; jOutput:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *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!
تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.
Copyright© 2022 湘ICP备2022001581号-3