S. Lam S. Lam - 2 months ago 8
C Question

How many rows for your star design?

I want to be able to print a flag like this when the user enters any number for the input:

Flag

Current Code:

printf("How many rows for your star design?\n");
scanf("%d", &num_rows);

printf("How many stars on the first row?\n");
scanf("%d", &num_symbols);

/*** Add your loops here ***/

for(i=0;i<num_rows;i++)
{
if(i%2 == 0)
{
for(j=num_symbols; i>=1; i--)
printf(" *");
printf("\n");

}
else if(i%2 == 1)
{
for(j=num_symbols; i>=0; i--)
printf("* ");
printf("\n");
}
}
return 0;


}

Answer

This is what you can do-

#include <stdio.h>

int main()
{
    int num_rows, num_symbols, i, j;
    printf("How many rows for your star design?\n");
    scanf("%d", &num_rows);
    printf("How many stars on the first row?\n");
    scanf("%d", &num_symbols);
    for(i=0;i<num_rows;i++)
    {
        if(i%2==0)
            for(j=num_symbols;j>0;j--)
                printf("* ");
        else
            for(j=num_symbols;j>1;j--)
                printf(" *");
        printf("\n");
    }
    return 0;
}

Good luck!

Comments