Categories: C++

Christmas Tree Pattern

C++ programming using “*” for Christmas Tree Pattern

Program code:

#include<iostream>
using namespace std;

int main()
{
 int width, height, i, j, k, n = 1;

 cout << "Enter Christmas Tree Width & Height : ";
 cin >> width >> height;

 int space = width * height;

 cout << "Christmas Tree \n";

 for (int x = 1; x <= height; x++)
 {
  for (i = n; i <= width; i++)
  {
   for (j = space; j >= i; j--)
   {
    cout <<" ";
   }
   for (k = 1; k <= i; k++)
   {
    cout <<"* ";
   }
   cout <<"\n";
  }
  n = n + 2;
  width = width + 2;
 }
 for (i = 1; i <= height - 1; i++)
 {
  for (j = space - 3; j >= 0; j--)
  {
   cout << " ";
  }
  for (k = 1; k <= height - 1; k++)
  {
   cout << "* ";
  }
  cout << "\n";
 }
}
Output:
Enter Christmas Tree Width & Height : 4 5
Christmas Tree 
                    * 
                   * * 
                  * * * 
                 * * * * 
                  * * * 
                 * * * * 
                * * * * * 
               * * * * * * 
                * * * * * 
               * * * * * * 
              * * * * * * * 
             * * * * * * * * 
              * * * * * * * 
             * * * * * * * * 
            * * * * * * * * * 
           * * * * * * * * * * 
            * * * * * * * * * 
           * * * * * * * * * * 
          * * * * * * * * * * * 
         * * * * * * * * * * * * 
                  * * * * 
                  * * * * 
                  * * * * 
                  * * * * 

Note: also read about Pattern Program Using Star

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

3 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

4 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

4 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

5 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

5 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

5 months ago