Star program in C
×


Star program in C

38

Example1: Right Triangle Star Pattern

// Program to Print a right triangle star pattern.
#include<stdio.h>

int main() {
    int rows, L, M;

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

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

    return 0;
}

Output:

Enter number of rows: 5
*
**
***
****
*****

Example 2: Inverted Right Triangle Star Pattern

// Program Print an inverted right triangle star pattern
#include<stdio.h>

int main() {
    int rows, L, M;

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

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

    return 0;
}

Output:

Enter number of rows in program: 5
*****
****
***
**
*
Example 3: Pyramid Star Pattern Description: Print a pyramid star pattern.

// Program Print a pyramid star pattern
#include<stdio.h> 

int main() {
    int rows, L, M, space;

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

    for (L = 1; L <= rows; L++) {
        // Print spaces
        for (space = 1; space <= rows - L; space++) {
            printf(" ");
        }

        // Initialize M for each row
        M = 1;

        // Print stars
        for (M = 1; M <= 2 * L - 1; M++) {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}

Output:

Enter number of rows: 4
   *
  ***
 *****
*******
Example 4: Hollow Rectangle Star Pattern 

// Program for printing a hollow rectangle star pattern
#include<stdio.h>

int main() {
    int rows, cols, L, M;

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

    printf("Enter number of columns: ");
    scanf("%d", &cols);

    for (L= 1; L<= rows; L++) {
        for (M = 1; M <= cols; M++) {
            if (L == 1 || L == rows || M == 1 || M == cols) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

Output:

Enter number of rows: 6
Enter number of columns: 6
******
*    *
*    *
*    *
*    *
******

Compile and run these programs to see the respective star patterns.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments