i want to create a pattern in c++ that looks like a trianlge(or half a diamond)
using asteriscks: the pattern should have 1, 2, 3, 4, and end in 5 stars like this
*
**
***
****
*****
(but straight!)
my code is as follows:
-`#include
using namespace std;
int main()
{
int size;
cout<<"size:"<<endl;
cin>>size;
int blank=size/2;
int newsize=1;
for (int i=0; i<=size/2; i++)
{
for(int j=blank;j>0;j--)
cout <<" ";
blank--;
for(int j=newsize; j>0; j--)
cout <<"*";
newsize+=2;
cout <<endl;
}
return 0;
}
`
my only problem with it is that it displays 1, 3,and 5 stars like this.
*
***
*****
its just a minor problem but although i have changed different parts of the code
i dont seem to get it right.
any suggestions?
thanks
🙂
I’m not sure what you mean by "but straight", so I’ll just ignore that for now…
Start with
blankthe same value assize, so that you can decrement the value each time without having to decrement by a half:Loop up to
sizeinstead ofsize/2to get more lines:Decrement by two in the loop for spaces to get half the number of spaces:
Increase the size by one instead of two to get the slower increase:
That should produce the output that you showed.
Edit:
I tested this to be sure, and the output is:
To get the exact output that you asked for, start with blank one less: