add 2 asterisks for each line

*
***
*****
.........
How to print such list of asterisks using a for loops. There are a total of n lines, n is a variable.

Thanks in advance!
You need two for loops. The first is i, going from 0 to <n, that contains a) a for loop from 0 to <=2*i in which you print a * symbol, and b) print a new line.
Say n=3.
i=0, j=0 will print a star, then the endline
i=1, j=0,1,2 will print 3 stars
i=2, j=0,1,2,3,4 will print 5 stars
1
2
3
4
5
6
7
8
9
for(int x=0; x<n ; x++) // loop for vertical lines or line number
{
   for( int y=0 ; y <= x ; y++) // prints number of * equal to line 
    {
       cout<< "*";
    }
   cout<<"**";  // prints the additional 2 * per line 
   cout<<'\n';
}

:D
Last edited on
guys please use code tags when posting
http://www.cplusplus.com/articles/z13hAqkS/

you only need one for loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	const int SIZE = 10;
	int numStar = 0;
	cout << setfill('*');


	for (int i = 0; i < SIZE; i++, numStar += 2)
	{
		cout << setw(numStar) << ' ' << endl;
	}

	cin.ignore();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.