Cover Image for Star Program in C
78 views

Star Program in C

Creating star patterns in C is a common exercise for practicing loops and control structures. You can create various patterns using nested loops. Here, I’ll show you how to create a few simple star patterns in C.

1. Printing a Square of Stars:

C
#include <stdio.h>

int main() {
    int i, j, rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= rows; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

2. Printing a Right Triangle of Stars:

C
#include <stdio.h>

int main() {
    int i, j, rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

3. Printing an Inverted Right Triangle of Stars:

C
#include <stdio.h>

int main() {
    int i, j, rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = rows; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

4. Printing a Pyramid of Stars:

C
#include <stdio.h>

int main() {
    int i, j, space, rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++) {
        for (space = 1; space <= rows - i; space++) {
            printf("  ");
        }
        for (j = 1; j <= 2 * i - 1; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

These are just a few examples of star patterns in C. You can modify and combine loops to create more complex patterns or customize the output according to your requirements. These patterns are helpful for practicing loop control structures and can be extended to create more intricate designs.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS