loops - print empty asterisk triangle c -


so i've looked , found normal triangles questions out there. 1 bit tricky.

given n, program should create asterisk unfilled triangle n side size.

example 1  inform n: 1  *  example 2: inform n: 2 ** * example 3: inform n: 3 *** ** * example 4: inform n: 4 **** * * ** * example 5: inform n: 5 ***** *  * * * ** * 

here's attempt, make filled triangle inefficiently

void q39(){     int n,i,b;     printf("inform n: ");     scanf ("%i",&n);     ( = 0; < n; ++i)     {     printf("*");     ( b = 1; b < n; ++b)     {         if (i==0)         {         printf("*");         }         else if (i==1 && b>1)         {             printf("*");         }         else if (i==2 && b>2)         {             printf("*");         }         else if(i==3 && b>3){             printf("*");         }         else if(i==4 && b>4){             printf("*");         }         else if(i==5 && b>5){             printf("*");         }         else if(i==6 && b>6){             printf("*");         }         else if(i==7 && b>7){             printf("*");         }         else if (i==8 && b>8){             printf("*");         }     }         printf("\n");     }  } 

you need think first line should filled *.
second thing first character of every line should *.
, last character should *. , in between need fill spaces.

int main() {     int n=6;     for(int i=n-1;i>=0;i--) // using n-1, bcz loop running upto 0     {         for(int j=0;j<=i;j++)         {             if(i==n-1 || j==0 ||i==j)                 printf("*");             else                 printf(" ");         }         printf("\n");           }     return 0; } 

the condition if(i==n-1 || j==0 ||i==j)
here i==n-1 used first line should filled *.
j==0 used make first character of every line *. every time when new line starts i.e j=0 print 1 * character.
i==j used make last character * when i==j last index upto running loop. @ last index print *.
, other values print space run else condition.

output

****** *   * *  * * * ** * 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -