Star Pattern Programs in C Programming

Introduction

Star pattern programs are essential for understanding loops in C programming. These programs involve the use of nested loops to print star (*) patterns in different shapes like triangles, pyramids, diamonds, and more. These patterns often come up in coding interviews and help to improve problem-solving and loop handling skills in C. In this post, we'll cover 15 star pattern programs in C with detailed explanations and code.


1. Right-Angled Triangle Star Pattern

This pattern forms a right-angled triangle using stars, with the right angle at the bottom-left corner.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Inner loop to print stars
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n"); // Move to the next line after printing each row
    }

    return 0;
}

Explanation:

  • The outer loop runs from 1 to rows, controlling the number of rows.
  • The inner loop runs from 1 to i, where i is the current row number, printing stars in each row.
  • Each row contains stars equal to the current row number, forming a right-angled triangle.

2. Inverted Right-Angled Triangle Star Pattern

This pattern prints an inverted right-angled triangle, starting with the maximum number of stars at the top and reducing with each row.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = rows; i >= 1; i--) {
        // Inner loop to print stars
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n"); // Move to the next line after printing each row
    }

    return 0;
}

Explanation:

  • The outer loop runs from rows down to 1, decreasing the number of stars in each row.
  • The inner loop prints stars for each row based on the value of i.
  • This forms an inverted right-angled triangle, starting with rows stars and reducing by one star in each subsequent row.

3. Pyramid Star Pattern

This pattern prints a pyramid of stars, where the stars are centered and the number of stars increases with each row.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Print leading spaces for alignment
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print stars in increasing order
        for (int j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }

        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The first inner loop prints spaces to center-align the stars.
  • The second inner loop prints stars. The formula (2 * i - 1) ensures that the number of stars increases by two for each row.
  • The outer loop controls the number of rows and how many spaces/stars should be printed in each row.

4. Inverted Pyramid Star Pattern

This pattern prints an inverted pyramid, starting with the maximum width of stars and gradually reducing the number of stars in each row.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = rows; i >= 1; i--) {
        // Print leading spaces for alignment
        for (int j = rows; j > i; j--) {
            printf(" ");
        }

        // Print stars in decreasing order
        for (int j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }

        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The first inner loop prints leading spaces to center the stars.
  • The second inner loop prints stars in decreasing order, using the formula (2 * i - 1) to reduce the number of stars as the rows decrease.
  • This forms an inverted pyramid that starts wide and narrows as it moves down.

5. Hollow Square Star Pattern

This pattern prints a hollow square, with stars only on the boundary of the square, and the inside is left hollow.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int size = 5;

    // Outer loop for rows
    for (int i = 1; i <= size; i++) {
        // Inner loop for columns
        for (int j = 1; j <= size; j++) {
            // Print stars on the boundary
            if (i == 1 || i == size || j == 1 || j == size) {
                printf("*");
            } else {
                printf(" "); // Print spaces inside the square
            }
        }
        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The outer loop runs from 1 to size, controlling the number of rows.
  • The inner loop runs from 1 to size, controlling the number of columns.
  • Stars are printed only on the boundary (first and last rows/columns), and spaces are printed inside to create the hollow effect.

6. Hollow Pyramid Star Pattern

This pattern prints a hollow pyramid with stars on the boundary and spaces inside.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Print leading spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print stars and spaces to form the hollow pyramid
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1) || i == rows) {
                printf("*");
            } else {
                printf(" ");
            }
        }

        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The first inner loop prints spaces to align the stars at the center.
  • The second inner loop prints stars at the boundary (first and last positions) and spaces inside to create a hollow pyramid.
  • The last row is fully filled with stars.

7. Diamond Star Pattern

A diamond shape is formed by combining two pyramids: one regular and one inverted.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Print the top pyramid
    for (int i = 1; i <= rows; i++) {
        for (int j = i; j < rows; j++) {
            printf(" ");
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    // Print the bottom inverted pyramid
    for (int i = rows - 1; i >= 1; i--) {
        for (int j = rows; j > i; j--) {
            printf(" ");
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  • The first part prints a regular pyramid with increasing stars.
  • The second part prints an inverted pyramid starting with the maximum number of stars and reducing row by row.
  • Together, they form a diamond shape.

8. Sandglass Star Pattern

This pattern resembles a sandglass, with an inverted pyramid at the top and a regular pyramid at the bottom.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Print the upper inverted pyramid
    for (int i = rows; i >= 1; i--) {
        for (int j = rows; j > i; j--) {
            printf(" ");
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
           

 printf("*");
        }
        printf("\n");
    }

    // Print the lower regular pyramid
    for (int i = 2; i <= rows; i++) {
        for (int j = rows; j > i; j--) {
            printf(" ");
        }
        for (int j = 1; j <= (2 * i - 1); j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  • The first part prints an inverted pyramid.
  • The second part prints a regular pyramid starting from row 2, thus forming a sandglass shape.

9. Right-Aligned Triangle Star Pattern

A right-aligned triangle pattern where stars are aligned to the right side of the console.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Print spaces for alignment
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print stars for each row
        for (int j = 1; j <= i; j++) {
            printf("*");
        }

        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The first inner loop prints spaces to right-align the stars.
  • The second inner loop prints stars for each row.
  • This forms a right-aligned triangle with the stars increasing as the rows increase.

10. Left-Aligned Triangle Star Pattern

This pattern forms a left-aligned triangle where stars increment as the rows increase.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {
        // Inner loop to print stars
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The outer loop controls the number of rows.
  • The inner loop prints stars for each row, starting from 1 and increasing by 1 in each row.
  • This forms a left-aligned triangle.

11. Hollow Diamond Star Pattern

A hollow diamond pattern with stars only on the boundary and spaces inside.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Print the upper half of the diamond
    for (int i = 1; i <= rows; i++) {
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Print the lower half of the diamond
    for (int i = rows - 1; i >= 1; i--) {
        for (int j = rows; j > i; j--) {
            printf(" ");
        }

        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1)) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  • The first part prints the upper half of the hollow diamond.
  • The second part prints the lower half, starting with one row less than the top half to create the diamond shape.
  • The boundary is filled with stars, and spaces are printed inside.

12. Inverted Left-Aligned Triangle Star Pattern

This pattern prints an inverted left-aligned triangle where the stars start from the maximum number in the first row and decrease with each row.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = rows; i >= 1; i--) {
        // Inner loop to print stars
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The outer loop starts from rows and decreases by 1 in each iteration.
  • The inner loop prints stars based on the current value of i.
  • This forms an inverted left-aligned triangle.

13. Hollow Inverted Pyramid Star Pattern

This pattern prints an inverted pyramid where only the boundary of the pyramid is filled with stars, and the inside is hollow.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = rows; i >= 1; i--) {
        // Print leading spaces for alignment
        for (int j = rows; j > i; j--) {
            printf(" ");
        }

        // Print stars and spaces for the hollow effect
        for (int j = 1; j <= (2 * i - 1); j++) {
            if (j == 1 || j == (2 * i - 1) || i == rows) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The outer loop controls the rows and reduces with each iteration.
  • The inner loops handle spaces and stars, printing stars only at the boundary and leaving the inside hollow.

14. Cross Star Pattern

This pattern prints a cross (X) using stars, where stars appear on both diagonals.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int size = 9; // Should be an odd number for symmetry

    // Outer loop for rows
    for (int i = 1; i <= size; i++) {
        // Inner loop for columns
        for (int j = 1; j <= size; j++) {
            // Print stars on the diagonals
            if (j == i || j == (size - i + 1)) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n"); // Move to the next line after each row
    }

    return 0;
}

Explanation:

  • The outer loop runs through each row.
  • The inner loop prints stars only on the diagonals by checking if the column index is equal to the row index or if the column is on the opposite diagonal (size - i + 1).

15. Butterfly Star Pattern

A butterfly-shaped pattern with stars forming wings on the left and right sides.

Example:

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

C Program:

#include <stdio.h>

int main() {
    int rows = 5;

    // Print the upper half of the butterfly
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        for (int j = i; j < rows; j++) {
            printf("  ");
        }
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    // Print the lower half of the butterfly
    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        for (int j = i; j < rows; j++) {
            printf("  ");
        }
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation:

  • The first part prints the upper wings of the butterfly using stars and spaces.
  • The second part prints the lower wings by decreasing the number of stars row by row.

Conclusion

These 15 C star pattern programs demonstrate how to create various shapes like triangles, pyramids, diamonds, and other complex patterns using nested loops. By practicing these patterns, you can improve your understanding of loops, conditional statements, and problem-solving skills in C programming. These patterns are often asked in coding interviews and serve as a foundation for mastering loops.

Comments