You do not need 3 loops - that is unnecessary:
int k, l;
for(k=1; k<=5; k++) // outer loop
{
for(l=0; l<k; l++) // inner loop
printf("*");
printf("\n");
}
The idea is simple keep printing stars in inner loop as long as l
is less than k
. So as the row-number increases (tracked by k
) so does the number of stars. Your fixed code:
for(k=1; k<=5; k++)
{
for(l=0; l<=k-1; l++) printf("*");
printf("\n");
}