Saturday, May 22, 2010

I want a C program to print a pattern?

Hi I want a C program to get a number from me and print a patter like this.


1


2 2


3 3 3 3


4 4 4 4 4 4


It should be simple and in C.


Thanks in advance.


Aishu.

I want a C program to print a pattern?
This is the modified form of the code by our Top Contributor without having to use command line arguments. Credits should go to him!!





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


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


void main()


{


int height, line, i;


clrscr();


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


for (i = 0; i %26lt; height - 1; ++i)


printf(" ");


printf("1\n");


for (line = 1; line %26lt; height; ++line)


{


for (i = 0; i %26lt; height - line - 1; ++i)


printf(" ");


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


printf("%d", line + 1);


printf(" ");


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


printf("%d", line + 1);


printf("\n");


}


getch();


}
Reply:well, try doin it on ur own...


hint: use 3 nested for loops, one for digits, one for lines , and one for alignment..


try doin it on ur own,


and if still, u don't get, reply me back, will make u understand how to do !





there's no point in asking for codes, u won't learn anything
Reply:u wont learn anything unless u do it on ur own.....show me ur code and i'll u whats wrong wid it
Reply:Look my dear aishu , I think it is the work given by ur instructor..It is very simple..The answer is given by one of the top contributor in ur answers list.But don't copy that..Look programming is an art..it is not the science...It is like fabricating our thoughts in to computer language.!! Try to do your own..if you don't get then mail me.. vishnusiva001@gmail.com
Reply:#include %26lt;stdio.h%26gt;


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





int main(int argc, char **argv) {


int height, line, i;





if (argc != 2) {


printf("Syntax: %s n\n", argv[0]);


printf(" where n is the height of the pyramid\n");


return -1;


}





height = atoi(argv[1]);





// Print the first line


for (i = 0; i %26lt; height - 1; ++i)


printf(" ");


printf("1\n");





// Print the rest of lines


for (line = 1; line %26lt; height; ++line) {


for (i = 0; i %26lt; height - line - 1; ++i)


printf(" ");


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


printf("%d", line + 1);


printf(" ");


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


printf("%d", line + 1);


printf("\n");


}


return 0;


}


No comments:

Post a Comment