Friday, July 31, 2009

C program - help needed, plz!!?

how to write a C program for printing the numbers in the following pattern?


1


1 1


1 1 1


1 1 1 1


1 1 1 1 1

C program - help needed, plz!!?
/* Try this dude then send me a message if this will not work. */








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


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





main() {





int i, j;


clrscr();





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


{


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


{


printf("1 ");


}


printf("\n");


}





getch();


return 0;


}
Reply:So boring, is it a homework?
Reply:This smells like a homework problem, so I'm just going to tell you how to approach it.





You basically want 2 loops, one that does each line, and the second that does the numbers *in* each line. So...





thisline = 0;


repeat (until some condition, possibly 'thisline = 10' or whatever)


count = 0;


while count %26lt;= thisline do {


print a 1. increment count.


done;


print a newline ( that's a "\n" in C)


thisline++;


end repeat
Reply:visit my blog http://codesbyshariq.blogspot.com
Reply:You will need to create a loop that adds one to the previous entered number.





After declaring the variables at the start of the program create a count and add one, if the + is before the var. the compiler adds one first, if it is after the compiler starts wth the variable then adds one. Not too important in this instance but a point to remember.





Remember also to end your loop by telling the program the required range.





When it comes to printing you need to include the printf statement. I realise this answer does not write the code for you but that is so you learn. If you need further help I suggest joining the Code Gear forums http://dn.codegear.com/cpp


No comments:

Post a Comment