C Program to Print Mirror of Right Angled Triangle
Pyramid Program in C Programming - Mirror Image of Right Angled Triangle
Print the Following Pattern of Pyramid
1 2 3 4 5 | * * * * * * * * * * * * * * * |
Program :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<stdio.h> int main() { char ch = '*'; int i, j, no_of_spaces = 4, spaceCount; for (i = 1; i <= 5; i++) { for (spaceCount = no_of_spaces; spaceCount >= 1; spaceCount--) { 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.