Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Wednesday 25 December 2013

Christmas Tree Program In C Language

Following C language code generates Christmas Tree as output. In below code we are used recursion, that reduces number of line of code. We make separate function to draw tree named "christmas(int)". And at the end of tree we print "Merry Christmas!".
You can modify variable "last_line_star" inside "christmas()" function to make more tree segment. In our example, there are 3 tree segment. You can change tree using "no" variable inside "main()" function and "last_line_star" variable inside "christmas()" function.


Output:
Christmas Tree Program Output
Christmas Tree Program Output

Source Code:


#include<stdio.h>
#include<conio.h>

void christmas(int);

void main()
{
    int no=3;

    clrscr();

    printf("\n\n");

    christmas(no);

    printf("\n\n\t\t\t\tMerry Christmas!");

    getch();
}
void christmas(int no)
{
    static int last_line_star = 7;
    int i, j, k;

    if( no > last_line_star )
        return;

    for( i = 1; i <= no; i++ )
    {
        printf("\t\t\t\t");
        for( k = last_line_star; k > i; k--)
            printf(" ");

        for( j = 1; j <= i; j++)
        {
            printf(" *");
        }
        printf("\n");
    }
    christmas( no + 2 );
}

0 comments :

Post a Comment