Sunday, August 2, 2009

Can anyone help me with a C program?

so what i have to do is make a pyramid of asteriks like this:





*


***


*****


*******


in c programming! Any help would be greatly appreciated :D





ps. does anyone know where i can download a C compiler?

Can anyone help me with a C program?
When you count the number of asterisk you will notice it


as increasing per line








* %26lt;- 1


** %26lt;- 2


*** %26lt;- 3


**** %26lt;- 4


***** %26lt;- 5





which mean you wouuld print


on the 1st row - 1 asterisk


on the 2nd row - 2 asterisk


on the 3rd row - 3 asterisk


on the 4th row - 4 asterisk


on the 5th row - 5 asterisk





This translate that you will need a nested for loop


Outer loop will count from 1 to 5


Inner loop will count from 1 to current row count





main () {


int row,asterik;





for (int row = 1; row %26lt;= 5; row++) {


for (asterisk = 1; asterisk %26lt;= row; asterisk++) {


printf("*");


}


printf("\n");


}


}


}











playing with the terminating condition of the inner loop


will display a different shape, try this instead of


terminating at asterisk %26lt;= row


terminate at asterisk %26lt;= 6 - row





main () {


int row,asterik;





for (int row = 1; row %26lt;= 5; row++) {


for (asterisk = 1; asterisk %26lt;= 6 - row; asterisk++) {


printf("*");


}


printf("\n");


}


}


}
Reply:printf(" * \n");


printf(" ** \n");


etc etc
Reply:The printf function should work for you.





printf("*\n"); is an example. Learn how to do it now, it only gets harder as you get further.





Did your book come with one? They should have some free ones out there.
Reply:an easy and scalable way to do this is to use a for loop





would be something like this:





int rows = 5;


for(int x = rows; x%26gt;0; x--){


for(int i=-1; i%26lt;rows-x; i++){


System.out.print("*");


}


System.out.print("\n");


}


}


}





which produces:


*


**


***


****


*****





this example is in java not c but you can see the basic syntax and convert to c
Reply:#include%26lt;stdio.h%26gt;





int main(void)


{


int x = 0;


int y = 0;





for (int x = 1; x %26lt;= 5; x++)


{


for (int y = 1; y != x; y++)


fprintf(stdout, "*");


}


return 0;


}





http://www.bloodshed.net/devcpp.html


No comments:

Post a Comment