15 September 2009

pyramid in C

-----------------------pyramid1.c-----------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char*argv[])
{
int i, j, level;
char ch = '*';

if(argc < 2)
{
printf("usage: %s <level> <<character>>\n", argv[0]);
exit(0);
}
level = atoi(argv[1]);
if(argv[2]) ch = argv[2][0];

for(i=0; i<level; i++)
{
for(j=i; j<level-1; j++)
printf(" ");

for(j=0; j<2*i+1; j++)
printf("%c ", ch);

printf("\n");
}


return 0;
}
--------------------------------------------------------

-----------------------pyramid2.c-----------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char*argv[])
{
int i, j, level;
char ch = '*';

if(argc < 2)
{
printf("usage: %s <level> <<character>>\n", argv[0]);
exit(0);
}
level = atoi(argv[1]);
if(argv[2]) ch = argv[2][0];

for(i=0; i<level; i++)
{
for(j=0; j<2*level-1; j++)
printf("%c ", (abs(level-1-j)<=i)?ch:' ');

printf("\n");
}


return 0;
}
--------------------------------------------------------

No comments:

Post a Comment