Signup/Sign In

C++ Program For Draw A Perfect Christmas Tree

In this tutorial, we will see how to make a perfect tree pattern using "*" in the C++ programming language.

C++ Program To Print Christmas Tree

#include<iostream>
using namespace std;

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

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

	int space = width * height;

	cout << "Printing Christmas Tree Pattern of Stars\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";
	}
}


Please Enter Christmas Tree Width & Height = 4 4
Printing Christmas Tree Pattern of Stars
*
* *
* * *
* * * *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * *
* * *
* * *

Conclusion

The important thing to keep in mind is the logic to place the empty spaces for making the tree else it's just almost the same as any other pattern.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.