loops

i saw my teacher she wrote some codes of loops and put the value "*" and it comes output as

**
***
*****
******
*******
********

how can i do this????
Hi,
> How can I do this???

1
2
3
4
5
6
int a, b;
for(a = 1; a <= ???; a++)
 {
     for(b = 1; b <= a; b++) cout << '*';
    cout << endl;
 }
Last edited on
Does that help? :)
You'll need a nested for loop. Outer loop controls the number of rows, inner loop controls how many stars are printed in each row.

So something like this:
1
2
3
4
5
6
7
8
9
   for(int i = 0; i < 5; ++i)
   {
       for(int j = 0; j <= i; ++j)
       {
           std::cout << "*";
       }

       std::cout << "\n";
   }
Topic archived. No new replies allowed.