Search⌘ K

Hacker Challenge: Christmas Tree

Explore how to create a Christmas tree pattern by mastering iteration and reusable functions in C++. Learn to print multiple triangles with parameterized height and range to build complex shapes efficiently. This lesson helps develop problem-solving skills in pattern creation and extends your ability to write modular and extensible code.

We'll cover the following...

Challenge

Your code should print the following shape:

h is the height of a single complete pyramid.

Triangle of height 10
Triangle of height 10

Implementation Directions

If you observe the top green part closely, which contains three green triangles, they have the same triangle being printed in different regions. Therefore, you should make a generic code that prints the triangles with a range and height passed as arguments. In the main part, you should call the function that prints the triangle three times with separate ranges. After printing the green part, the trunk is a rectangle with the mentioned dimension and then followed by a floor.

C++
#include <iostream>
using namespace std;
void christmasTree(int h)
{
cout<<"Height:"<<h<<endl;
// write your coder here
}