Friday, May 21, 2010

C Program Help?

i'm supposed to write a program that prints the following patterns separately , one below the other. Use for loops to generate the patterns. All Asterisks (*) should be printed by a single printf statement of the form printf("*"); (this causes the asterisks to print side by side).





(A)


*


**


***


****


*****


******


*******


********


*********


**********





(B)


**********


*********


********


*******


******


*****


****


***


**


*





i have this so far:





#define N 4


main()


{


int i,j;


for(i=1; i%26lt;=N; i++);


{


for(j=1; j%26lt;=1; j++);


putchar('*')


putchar("\n");


}


}








help anyone?

C Program Help?
#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





//*star-1*//


main()


{


int i,j,n ;


clrscr() ;


printf("enter n") ;


scanf("%d",%26amp;n) ;


for(i=1;i%26lt;=n;i++)


{


for(j=1;j%26lt;=i;j++)


{


printf("*") ;


}


printf("\n") ;


}





//*star-2*//


printf("enter n") ;


scanf("%d",%26amp;n) ;


for(i=1;i%26lt;=n;i++)


{


for(j=1;j%26lt;=n-i+1;j++)


{


printf("*") ;


}


printf("\n") ;


}





getch();


}








Hope u've got it 'n I've helped u, frnd... bye.
Reply:// don't do this... just showing off


int m, c, d, i, j, k;


c = 2;


d = 10;


for( m=0; m%26lt;2; m++ )


for( i=0; i%26lt;c; i++ )


for( j=i%2?d:1; i%2?j:j%26lt;d; i%2?j--:j++ )


{


for( k=0; k%26lt;d; k++ )


putch( m?(k%26lt;j?' ':'*'):(k%26lt;j?'*':' ') );


putch( '\n' );


}


getch();
Reply:Hi, here's the solution:





#include %26lt;stdio.h%26gt;


#define N 8


int main(int argc, char **argv)


{


. . . . int i, j;


. . . . for(i = 0; i %26lt; N; i++)


. . . . {


. . . . . . . . for(j = 0; j %26lt;= i; j++)


. . . . . . . . {


. . . . . . . . . . . . printf("*");


. . . . . . . . }


. . . . . . . . printf("\n");


. . . . }


. . . . printf("\n");


. . . . for(i = 0; i %26lt; N; i++)


. . . . {


. . . . . . . . for(j = 0; j %26lt; N - i; j++)


. . . . . . . . {


. . . . . . . . . . . . printf("*");


. . . . . . . . }


. . . . . . . . printf("\n");


. . . . }


. . . . return 0;


}





Idea behind it:





The first big for loop prints out the first pattern. For this pattern you have N lines, number them 0, 1, ..., N-1. Then on the i'th line, you want to print out exactly i+1 asterisks. This is what the inner loop does. After the line is done printing, i.e. the inner loop finishes, you skip to the next line with prinft("\n").





The second big for loop prints out the second pattern. For this one you again have N lines to print out, number them 0, 1, ..., N-1. Then on the i'th line, you want to print out exactly N-i asterisks. The inner loop does this. The printf("\n") skips to the next line just like before.
Reply:you are at right track


put


j=o;j%26lt;=i;j++


in second loop


for b use the oppoite of the above


start from i=N; i%26gt;0; i--


j=i; j%26gt;0; j--


yes it is good start for you


No comments:

Post a Comment