In this tutorial, we will write a program to display half pyramid patterns in C programming language. We will create a star pattern in C using for loop. So you may go through the following topic in C.
1. Right half pyramid in C
Right angle triangle pattern in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; ++i) { for (j = 1; j <= i; ++j) { printf("* "); } printf("\n"); } return 0; } |
Enter the no. of rows: 5
*
* *
* * *
* * * *
* * * * *
2. Left half pyramid in C
Left right angle triangle pattern in C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> int main() { int i, j, k, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= rows - i; j++) printf(" "); for (k = 1; k <= i; k++) printf("*"); printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 | Enter the no. of rows: 5 * ** *** **** ***** |
3. Inverted right half pyramid pattern in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; --i) { for (j = 1; j <= i; ++j) printf("* "); printf("\n"); } return 0; } |
Enter the no. of rows: 6
* * * * * *
* * * * *
* * * *
* * *
* *
*
4. Inverted left half pyramid pattern in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> int main() { int i, j, rows; printf("Enter the no. of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= rows; j++) { if (j < i) printf(" "); else printf("*"); } printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 7 8 9 | Enter the no. of rows: 8 ******** ******* ****** ***** **** *** ** * |