c - How to print star pattern using only 2 for loops -
i want print pattern [half diamond shape]
* * * * * *
by using 2 loops easy print pattern using 3 loops
#include<conio.h> #include<stdio.h> void main() { clrscr(); int i,j,k; for(i=0;i<3;i++) //loop number of lines { for(j=3;j>i;j--) // loop printing _ { printf(" "); } for(k=0;k<=i;k++) // loop printing *_ { printf("* "); } printf("\n"); } getch(); }
so plz me......
int i,j; for(i=0;i<n;++i){ printf("%*s", n-i-1, "");//field width specification for(j=0;j<=i;++j){ printf("*"); if(j<i) printf(" "); } printf("\n"); }
#define n 3 ... char line[(n-1)+1+2*(n-1)]={0};//pre , *, "* "*(n-1) int i,j,k; for(k=0, i=n-1;k<n;i+=2,++k){ line[i] = '*'; for(j=k;j<=i;++j){ putchar(line[j] ? line[j] : ' '); } putchar('\n'); }
Comments
Post a Comment