c++ triangle in a nested while loop, simple ways, using counters

closed account (41U4izwU)
Hi, I am a beginner at coding and I recently received a question from my teacher, to make triangles using c++, in nested while loops.

The triangles were:
*
**
***
****
*****
******
*******
********
*********
********** \\ I have made a code for this:

int counter=1;

while (counter<=10){

int counter2=1;

while (counter2<counter){

cout<<"*";

counter2=counter2+1;

}



cout<<"*"<<"\n";

counter=counter+1;

}


**********
--*********
----********
------*******
--------******
----------*****
------------****
--------------***
----------------**
------------------*
I was not quite sure about this one.

Thanks for taking your time in helping me with this question.
Last edited on
1
2
3
4
5
6
7
8
9
10
int counter = 10;
while (counter > 0){
    int counter2 = 1;
    while (counter2 <= counter) {
        cout << '*';
        counter2 += 1;
    }
    cout << endl;
    counter -= 1;
}
closed account (41U4izwU)
Thanks a lot for helping me, i really appreciate it :)
closed account (41U4izwU)
oh, oops i think that you may have looked at the wrong triangle because it made it line up to the side...
1
2
3
4
5
6
7
8
9
10
11
12
13
int counter = 10;
while (counter > 0){
    int counter2 = 10;
    while (counter2 > 0) {
        if (counter2 > counter)
            cout << ' ';
        else
           cout << '*';
        counter2 -= 1;
    }
    cout << endl;
    counter -= 1;
}
closed account (41U4izwU)
sorry for the mistake, thanks again :)
Topic archived. No new replies allowed.