Program for Christmas Tree in C

In C programming, creating a Christmas tree pattern involves printing pyramids of various sizes stacked one beneath another. This creates a festive tree-like structure with decorative characters that can simulate twinkling lights through randomization.

Syntax

void tree_triangle(int start, int end, int total_height);
void display_tree(int height);
void display_log(int height);

Example: Static Christmas Tree

Let's start with a basic Christmas tree that displays once −

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void display_random_leaf() {
    char type_of_leaves[5] = { '.', '*', '+', 'o', 'O' };
    int temp = rand() % 5;
    if (temp == 1)
        printf("%c ", type_of_leaves[rand() % 5]);
    else
        printf("%c ", type_of_leaves[1]); /* mostly print * */
}

void tree_triangle(int f, int n, int toth) {
    int i, j, k = 2 * toth - 2;
    
    for (i = 0; i < f - 1; i++)
        k--;
        
    for (i = f - 1; i < n; i++) {
        for (j = 0; j < k; j++)
            printf(" ");
        k = k - 1;
        
        for (j = 0; j <= i; j++)
            display_random_leaf();
        printf("<br>");
    }
}

void display_tree(int h) {
    int start = 1, end = 0, diff = 3;
    while (end < h + 1) {
        end = start + diff;
        tree_triangle(start, end, h);
        diff++;
        start = end - 2;
    }
}

void display_log(int n) {
    int i, j, k = 2 * n - 4;
    for (i = 1; i <= 6; i++) {
        for (j = 0; j < k; j++)
            printf(" ");
        for (j = 1; j <= 6; j++)
            printf("#");
        printf("<br>");
    }
}

int main() {
    srand(time(NULL));
    int ht = 10;
    
    printf("Christmas Tree:<br><br>");
    display_tree(ht);
    display_log(ht);
    
    return 0;
}
Christmas Tree:

         * 
        * * 
       * * * 
        * * 
       * * * 
      * * * * 
       * * * 
      * * * * 
     * * * * * 
      * * * * 
     * * * * * 
    * * * * * * 
     * * * * * 
    * * * * * * 
   * * * * * * * 
    * * * * * * 
   * * * * * * * 
  * * * * * * * * 
   * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 
  * * * * * * * * 
 * * * * * * * * * 
* * * * * * * * * * 
 * * * * * * * * * 
* * * * * * * * * * 
      ######
      ######
      ######
      ######
      ######
      ######

How It Works

  • tree_triangle() − Creates individual pyramid sections with proper spacing
  • display_tree() − Stacks multiple triangles to form the tree shape
  • display_log() − Prints the trunk using '#' characters
  • display_random_leaf() − Adds variety by randomly selecting decorative characters

Example: Simple Tree Pattern

Here's a simplified version that creates a basic Christmas tree pattern −

#include <stdio.h>

void print_spaces(int count) {
    for (int i = 0; i < count; i++) {
        printf(" ");
    }
}

void print_stars(int count) {
    for (int i = 0; i < count; i++) {
        printf("* ");
    }
}

void print_tree(int height) {
    /* Print tree sections */
    for (int section = 0; section < 3; section++) {
        for (int row = 0; row < height - section; row++) {
            print_spaces(height - row + section);
            print_stars(row + 1 + section);
            printf("<br>");
        }
    }
    
    /* Print trunk */
    for (int i = 0; i < 3; i++) {
        print_spaces(height);
        printf("|||<br>");
    }
}

int main() {
    int height = 5;
    
    printf("Simple Christmas Tree:<br><br>");
    print_tree(height);
    
    return 0;
}
Simple Christmas Tree:

     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
    * * 
   * * * 
  * * * * 
 * * * * * 
   * * * 
  * * * * 
 * * * * * 
     |||
     |||
     |||

Note: The flickering animation effect shown in the original requires system-specific screen clearing functions that won't work in online compilers. The above examples show the static tree pattern structure.

Key Points

  • Christmas trees use multiple pyramid sections stacked together
  • Proper spacing calculation ensures center alignment
  • Random character selection creates decorative variety
  • The trunk is typically 3-6 rows of repeated characters

Conclusion

Creating Christmas tree patterns in C demonstrates nested loops, spacing calculations, and pattern generation. The basic structure involves stacked triangular sections with a trunk at the bottom.

Updated on: 2026-03-15T10:41:18+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements