Pyramid Program in C Programming - Mirror Image of Right Angled Triangle

January 18, 2025 No Comments » Hits : 357





Pyramid Program in C Programming - Mirror Image of Right Angled Triangle

Print the Following Pattern of Pyramid


Program :


#include<stdio.h>
int main()
{
char ch = '*';
int i, j, no_of_spaces = 4, space_count;
for (i=1; i<=5; i++)
{
    for(space_count = no_of_spaces; space_count >= 1; space_count--)
    {
    printf(""); //2spaces
    }
    for(j=1; j<=i; j++)
    {
    printf("%2c",ch);
    }
printf("\n");
no_of_spaces--;
}
return 0;
}

Explanation of C Program :

  1. space_count‘ variable is used as subscript variable used in for loop.
  2. no_of_spaces‘ variable is used to keep track of no_of_spaces to be printed.
  3. Note that Space width mentioned in printf of second inner loop is “%2c” , that’s why we have printed 2 spaces inside first inner loop.

Why 3 for loops are used ?

  • Outer For Loop - Specify No of Rows to be Printed
  • First Inner For Loop - Specify How many Spaces is to be printed in a particular row.
  • Second Inner For Loop - Specify no of ‘*’ to be printed on the same line.

Incoming search terms: