星期六, 4月 10, 2010

Simple codes to build a pyramid

Someone asked me how to write a program to build "*" pyramid when reading this article. However, I didn't learn java since that time, so I wrote some simple codes in C language. The concept is very simple because it can be done by using nest loop.

##CONTINUE##
Here is the code to build "*" pyramid.
#include 
int main()
{
    int i,j;
    for (j=1; j<=5; j++)
    {
        for (i=1; i<=5; i++)
        {
            if (i<5-j+1)
            {
                printf(" ");
            }
            else
            {
                printf("* ");
            }
        }
        printf("\n");
    }

    return 0;
}

You can also arrange the "*" on the left side.
#include 
int main()
{
    int i,j;
    for (j=1; j<=10; j++)
    {
        for (i=1; i<=10; i++)
        {
            if (i<10-j+1)
            {
                printf("  ");
            }
            else
            {
                printf("* ");
            }
        }
        printf("\n");
    }

    return 0;
}