Pyramid Patterns in C
×


Pyramid Patterns in C

321

1 Left-aligned Pyramid Pattern

This pattern is a left-aligned triangle where each row contains an increasing number of asterisks.

Program for left-aligned pyramid pattern in C

 // Program for left-aligned pyramid pattern in C
#include<stdio.h> 

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

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

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

    return 0;
}

Output:

Enter number of rows: 4
* * * * * * * * * *

2 Right-aligned Pyramid Pattern

This pattern is a right-aligned triangle where each row contains an increasing number of asterisks.

Program for Right-aligned pyramid pattern in C

// Program for Right-aligned pyramid pattern in C
#include<stdio.h>

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

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

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

    return 0;
}
	

Output:

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

3 Inverted Pyramid Pattern

This pattern is an upside-down triangle where each row contains a decreasing number of asterisks.

Program for Inverted pyramid Pattern in C

// Program for Inverted pyramid Pattern in C
#include<stdio.h> 

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

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

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

    return 0;
}
		

Output:

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

4 Hollow Pyramid Pattern

This pattern is a solid pyramid with spaces in the middle, creating a hollow effect.

Program for Hollow pyramid pattern in C

#include<stdio.h>

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

    printf("Enter 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++) {
            if(j == 1 || j == 2 * i - 1 || i == rows)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }

    return 0;
}
			

Output:

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

You can try out these patterns and even modify the programs to create more variations or customize the patterns further.



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