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 :
- ‘space_count‘ variable is used as subscript variable used in for loop.
- ‘no_of_spaces‘ variable is used to keep track of no_of_spaces to be printed.
- 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.