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

Mar 13, 2013 at 8:07pm
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 Mar 13, 2013 at 8:15pm
Mar 13, 2013 at 8:11pm
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;
}
Mar 13, 2013 at 8:12pm
closed account (41U4izwU)
Thanks a lot for helping me, i really appreciate it :)
Mar 13, 2013 at 8:16pm
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...
Mar 13, 2013 at 8:38pm
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;
}
Mar 13, 2013 at 8:42pm
closed account (41U4izwU)
sorry for the mistake, thanks again :)
Topic archived. No new replies allowed.