Â
This is a for loop program in which we are going to see how to print the above pattern of pyramid/triangle of special character, with the help of the C Programming language.
#include <stdio.h>
void main()
{
int rows;
printf("\nEnter the number of rows\n");
scanf("%d", &rows);
for (int i = 0; i <= rows - 1; i++)
{
//This loop is to print spaces in begining for the required pattern
for (int j = 1; j <= rows - i + 1; j++)
{
printf(" ");
}
//This loop is for the 1st half of the pyramid
for (int k = 0; k <= i; k++)
{
if (i % 2 == 0)
{
printf("# ");
}
else
{
printf("# ");
}
}
//This loop is for the 2nd half of the pyramid
for (int l = i - 1; l >= 0; l--)
{
if (i % 2 == 0)
{
printf("# ");
}
else
{
printf("# ");
}
}
printf("\n");
}
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP