Code :
#include <stdio.h>int main(){ int i, j, k; for (i = 1; i <= 4; i++) { for (j = 5; j >= i; j--) { printf(" "); } for (k = 1; k <= (i*2)-1; k++) { if (k==1 || k==(i*2)-1) { printf("*"); } else{ printf(" "); } } printf("\n"); }
for (i = 5; i >= 1; i--) { for (j = 5; j >= i; j--) { printf(" "); } for (k = 1; k <= (i*2)-1; k++) { if (k==1 || k==(i*2)-1) { printf("*"); } else{ printf(" "); } } printf("\n"); } return 0;}
-
Header File:
#include <stdio.h>
is needed to useprintf()
for printing output. -
Main Function: The program starts from
int main()
. It returns 0 to indicate successful completion. -
First Outer Loop (Top Section): The first outer
for
loop (i = 1 to i <= 4
) controls the rows of the top section, with spaces decreasing and stars being printed at the borders of each row, creating a hollow triangle shape. -
Inner Loop 1 (Spaces for Top Section): The first inner
for
loop (j = 5 to j >= i
) prints spaces, decreasing the number of spaces as the row number increases. -
Inner Loop 2 (Hollow Stars for Top Section): The second inner
for
loop (k = 1 to k <= (i * 2) - 1
) prints stars (*
) only at the first and last positions, leaving spaces in between to create the hollow effect. -
Second Outer Loop (Bottom Section): The second outer
for
loop (i = 5 to i >= 1
) controls the rows of the bottom section, with the stars forming a hollow inverted triangle. -
Inner Loop 1 (Spaces for Bottom Section): The first inner
for
loop (j = 5 to j >= i
) prints spaces, increasing as the row number decreases. -
Inner Loop 2 (Hollow Stars for Bottom Section): The second inner
for
loop (k = 1 to k <= (i * 2) - 1
) prints stars (*
) only at the first and last positions, leaving spaces in between, similar to the top section. -
New Line:
printf("\n");
moves the output to a new line after printing each row.
Output :
* * * * * * * * * * * * * * * *