New: 203 pattern programs with tested code in C, C++, Java and Python Explore patterns

Christmas Tree Pattern

Read summary with AI tools

The Christmas tree pattern stacks a few small pyramids so that each one is a little wider than the one above, then adds a trunk. It is a favourite in December coding contests and a good test of centring text.

Quick answer: Treat the tree as tiers. Tier t (from 0) has three rows with 2(r + t) + 1 stars on row r, so every tier starts two stars wider than the last one did. Centre each row by printing (width − stars) / 2 spaces first, where width is the widest row. Finish with two rows of ||| for the trunk.

This program is part of the Tricky Patterns collection.

Table of contents8 sections
  1. Output
  2. How the christmas tree pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Christmas Tree Pattern output

Output (3 tiers):

    *
   ***
  *****
   ***
  *****
 *******
  *****
 *******
*********
   |||
   |||
Christmas Tree Pattern layout diagram, row by row (3 tiers)
How each row of the christmas tree pattern is built (3 tiers). Blue cells are printed characters; grey dots are spaces.

How the christmas tree pattern works

The difference from a plain pyramid is that the star count goes back down at the start of each tier. Adding the tier number t to the row number r handles that: tier 0 prints 1, 3, 5 stars, tier 1 prints 3, 5, 7 and tier 2 prints 5, 7, 9. Because rows of different tiers need different indents, the program works out the widest row once and centres every row against it. The trunk uses the same centring with a width of 3.

  1. Work out the widest row: width = 2(n + 1) + 1 for n tiers of three rows.
  2. For each tier t and each of its rows r, set stars = 2(r + t) + 1.
  3. Print (width − stars) / 2 spaces, then the stars, then a new line.
  4. Print the trunk: two rows of |||, each indented by (width − 3) / 2 spaces.

Key takeaways

  • Each tier is a small pyramid that starts wider than the one above.
  • Centring against one fixed width keeps every tier on the same middle line.
  • The trunk is centred with the same formula, using its own width of 3.
  • In C++ and Java, string(k, ' ') and " ".repeat(k) print a run of characters without an inner loop.

Christmas Tree Pattern in C

#include <stdio.h>

int main(void) {
    int n = 3;                              /* tiers, each 3 rows tall */
    int width = 2 * (n + 1) + 1;            /* widest row of the bottom tier */

    for (int t = 0; t < n; t++) {
        for (int r = 0; r < 3; r++) {
            int stars = 2 * (r + t) + 1;
            for (int k = 0; k < (width - stars) / 2; k++)
                printf(" ");
            for (int k = 0; k < stars; k++)
                printf("*");
            printf("\n");
        }
    }
    for (int r = 0; r < 2; r++) {           /* trunk */
        for (int k = 0; k < (width - 3) / 2; k++)
            printf(" ");
        printf("|||\n");
    }
    return 0;
}

Christmas Tree Pattern in C++

#include <iostream>
#include <string>
using namespace std;

int main() {
    int n = 3;                              // tiers, each 3 rows tall
    int width = 2 * (n + 1) + 1;            // widest row of the bottom tier

    for (int t = 0; t < n; t++) {
        for (int r = 0; r < 3; r++) {
            int stars = 2 * (r + t) + 1;
            cout << string((width - stars) / 2, ' ') << string(stars, '*') << endl;
        }
    }
    for (int r = 0; r < 2; r++)             // trunk
        cout << string((width - 3) / 2, ' ') << "|||" << endl;
    return 0;
}

Christmas Tree Pattern in Java

public class ChristmasTree {
    public static void main(String[] args) {
        int n = 3;                              // tiers, each 3 rows tall
        int width = 2 * (n + 1) + 1;            // widest row of the bottom tier

        for (int t = 0; t < n; t++) {
            for (int r = 0; r < 3; r++) {
                int stars = 2 * (r + t) + 1;
                System.out.println(" ".repeat((width - stars) / 2) + "*".repeat(stars));
            }
        }
        for (int r = 0; r < 2; r++)             // trunk
            System.out.println(" ".repeat((width - 3) / 2) + "|||");
    }
}

Christmas Tree Pattern in Python

n = 3                                   # tiers, each 3 rows tall
width = 2 * (n + 1) + 1                 # widest row of the bottom tier

for t in range(n):
    for r in range(3):
        stars = 2 * (r + t) + 1
        print(" " * ((width - stars) // 2) + "*" * stars)
for r in range(2):                      # trunk
    print(" " * ((width - 3) // 2) + "|||")

Time and space complexity

The tree prints O(n²) characters for n tiers, one at a time, and needs only a few counters, so the extra memory is O(1).

Frequently asked questions

How do I add more tiers?

Raise n. The width formula grows with it, so each new tier still lines up under the others. Five tiers make a tree 13 stars wide at the base.

Can I make the tiers taller?

Yes. Replace the 3 in the row loop with a variable such as h and change the width to 2(h + n − 2) + 1. Every row is still centred against the widest one.

How do I decorate the tree?

Print a different character on some positions, for example o wherever (r + k) % 4 == 0. Keep the same counts and only change which character goes where, as in the checkerboard pattern.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.