asterisks in a form of triangles

I am trying to write a program that prints asterisks in a form of triangles. I would like to print 4 different triangles. I managed to do the first two. I would like to do two more triangles, these should be the vertical mirror image of the first two triangles (right justified, but I would like to do it with cout << " ").

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
51
52
53
54
55
56
57
58
59

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
unsigned int x;
unsigned int y;

unsigned int o{1};
for(x=1; x<=10; x++){
    cout << "\n";
    
    for(y=o; y>=1; y--){
        cout << "*";
    }
    o++;
}

cout << "\n";

unsigned int n{10};
for(x=1; x<=11; x++){
    cout << "\n";
    
    for(y=n; y>=1; y--){
        cout << "*";
        
    }
    n--;
}

unsigned int p{10};
for(x=1; x<=10; x++){
    cout << " " << "\n";
    
    for(y=p; y>=1; y--){
        cout << "*";
        
    }
    p--;
}

cout << "\n";

unsigned int u{1};
unsigned int f{1};
for(x=1; x<=10; x++){
    cout << "\n";
    
    for(y=u; y>=1; y--){
        cout << "*";
    }
    u++;
}
}
        
  
  
This is a basic exercise for understanding nested loops. You are doing okay, except for the printing part.

Remember, you need to print not just asterisks, but spaces.

It helps to get out a piece of graph paper and a pencil and draw what you want to see, treating each square cell of the paper as a character cell of your terminal.

Good luck!
Yes, I got that, I need to put spaces before the asterisks. If I do it with another for statement it will be in a separate row and I can`t put the spaces with the asterisks in a cout statement neither.
That is the closest result I have got so far. Please if anyone has an idea how I can make those triangles. Please help me.

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
51
52
53
54
55
56
57
58
59
60
61

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
unsigned int x;
unsigned int y;


unsigned int o{1};
for(x=1; x<=10; x++){
    cout << "\n";
    
    for(y=o; y>=1; y--){
        cout << "*";
    }
    o++;
}

cout << "\n";

unsigned int n{10};
for(x=1; x<=11; x++){
    cout << "\n";
    
    for(y=n; y>=1; y--){
        cout << "*";
        
    }
    n--;
}

cout << "\n";

for(x=1; x<=10; x++){
    for(y=1; y<=x; y++){
        if(x==y){
        cout << "*";
        }
        else{
        cout << " ";
        }
    }
    cout << "\n";
}

cout << "\n";


for(x=10; x>=1; x--){
    cout << "\n";
    
    for(y=1; y<=x; y++){
        cout << "*";
    }

}

}
       
Well it would be a hell of a lot easier if you chose meaningful variable names.
1
2
3
4
5
6
7
for ( int row = 0 ; row < 5 ; row++ ) {
  int numOfLeadingSpaces = ....
  int numOfStars = ....
  for ( int c = 0 ; c < numOfLeadingSpaces ; c++ ) cout << " ";
  for ( int c = 0 ; c < numOfStars ; c++ ) cout << "*";
  cout << "\n";
}


x,y,n,o makes your code read like alphabet soup.
Registered users can post here. Sign in or register to post.