C Program to Print Star Patterns Using Nested Loops

Introduction

Learning to use loops in C is a fundamental step in mastering programming logic. One of the best ways to understand nested loops is by creating star patterns. In this article, we will explore how to write a C program to print star patterns using nested loops. These patterns include a right-angle triangle, a centered pyramid, and an inverted triangle.


Problem Description

We are required to write a C program that prints the following three patterns:

Pattern i)

*
* *
* * *
* * * *
* * * * *

Pattern ii)

    *
* *
* * *
* * * *
* * * * *

Pattern iii)

*****
****
***
**
*

C Program to Print Star Patterns

#include <stdio.h>

int main() {
int i, j;

// Pattern i) Right-angled triangle
printf("Pattern i)\n");
for(i = 1; i <= 5; i++) {
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

// Pattern ii) Centered Pyramid
printf("\nPattern ii)\n");
for(i = 1; i <= 5; i++) {
for(j = 1; j <= 5 - i; j++) {
printf(" ");
}
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

// Pattern iii) Inverted triangle
printf("\nPattern iii)\n");
for(i = 5; i >= 1; i--) {
for(j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}

return 0;
}

Explanation of the Program

Pattern i)

This pattern uses an outer loop for rows and an inner loop to print stars. The number of stars increases with each row.

Pattern ii)

To create a pyramid, we first print spaces to center the stars. The number of spaces decreases as the row number increases.

Pattern iii)

This pattern starts with the maximum number of stars in the first row and decreases with each row using a reverse loop.


Why Use These Patterns?

  • Reinforces logic building with loops.
  • Improves understanding of nested loops.
  • Useful in interviews and academic practice.

Conclusion

Practicing star patterns is a great way to build a strong foundation in C programming. This C program to print star patterns demonstrates how nested loops work and helps develop logical thinking required in more complex applications.

Previous Article

C Program to Evaluate the Series 1 - x + x²/2! - x³/3! + … - x⁹/9!

Next Article

C Program to Print Number Patterns Using Nested Loops

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨