Half of the Christmas tree

Hello guys!

I'm really struggling how to get a half of a christmas tree, like if you'd cut a tree in a half then print out only the left side.

I've managed to do it but in a very uggo way.

I know that there must be some pattern in this and I know that I need
to make a for loop to print one star then two starts etc. but I can't figure it out for the world! I'm giving aways my solution and I'll appreciate your help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {
    
    int j = 5;
    for (int i = 0; i < j; ++i)
    {
        cout << " ";
    }
    
    cout << "*\n";
    j--;
    
    for (int i = 0; i < j; ++i)
    {
        cout << " ";
    }
    
    cout << "**\n";
    j--;
    
    for (int i = 0; i < j; ++i)
    {
        cout << " ";
    }
    
    cout << "***\n";
    j--;

    for (int i = 0; i < j; ++i)
    {
        cout << " ";
    }
    
    cout << "****\n";
    j--;

    for (int i = 0; i < j; ++i)
    {
        cout << " ";
    }
    
    cout << "*****\n";
    j--;

    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main()
{
    int maxNumberOfStars = 5;
    for (int i = 1; i <= maxNumberOfStars; i++)
    {
        for (int j = 0; j < maxNumberOfStars - i; j++)
            std::cout << " ";
        for (int j = 0; j < i; j++)
            std::cout << "*";
        std::cout << std::endl;
    }
}
Thank you!
#include <iostream>
int main()
{
int i=0, j=0;

for(int i=0;i<=5;i++)
{
for( int j=0;j<=i;j++)
{

std::cout<<"*";
}
std::cout<<std::endl;
}

return 0;
}
Last edited on
Topic archived. No new replies allowed.