Draw a Perfect Christmas Tree Using C++

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to create a perfect Christmas tree pattern using "*" in the C++ programming language. We will go through a step-by-step process to achieve this. The code will be shown in C++ language and explained for ease of understanding.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") subgraph Lab Skills cpp/variables -.-> lab-96160{{"`Draw a Perfect Christmas Tree Using C++`"}} cpp/data_types -.-> lab-96160{{"`Draw a Perfect Christmas Tree Using C++`"}} cpp/operators -.-> lab-96160{{"`Draw a Perfect Christmas Tree Using C++`"}} cpp/for_loop -.-> lab-96160{{"`Draw a Perfect Christmas Tree Using C++`"}} end

Define variables and Get User Input

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

The first step is to define variables and get user input for the width and height of the Christmas tree. In this step, we also define the space variable, which is equal to width*height.

#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;
}

Create the Christmas Tree Pattern

In this step, we create the Christmas tree pattern. We use three loops. The outermost loop iterates from 1 to height. The first inner loop prints the spaces required to center the tree. The second inner loop prints the "*"s.

    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;
    }

Create the Tree Base

In this step, we create the base for the tree. We use two loops. The first loop iterates over height-1, and the second loop prints "*"s required for base.

    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";
    }

To run the code in the terminal:

g++ main.cpp -o main && ./main

Summary

In this lab, we have learned how to create a perfect Christmas tree pattern using "*" in the C++ programming language. We have gone through the process step-by-step and explained the code for ease of understanding. We hope that you find this lab helpful!

Other C++ Tutorials you may like