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.
#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.
#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:
Enter the no. of rows: 5
*
**
***
****
*****
3. Inverted right half pyramid pattern in C
#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
#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:
Enter the no. of rows: 8
********
*******
******
*****
****
***
**
*