help with code

I have this code that make this types of triangle with starts:
*
**
***
****
*****

#include <iostream>

using namespace std;

int main()
{
int n;

cout<<"Enter a number: ";

cin>>n;

for (int a = 1; a <= n; a++)
{
for (int b = 1; b <= a; b++)
{
cout<<b<<" ";
}

cout<<endl;
}

return 0;
}
  
     *
   ***
 *****
******* 

The webpage messed up so I will try to explain how the triangle is supposed to look like. I basically a per with there only being one star on top then on the bottom 2 more EX.(1,3,5,7,9) ext... The user inputs how many rows.
Last edited on
There be no star in that code.

but I would guess you want..

for (int b = 1; b <= a+2; b++)

of course 2 can be a variable like +numstarz

Last edited on
My bad i copied my wrong code
#include <iostream>

using namespace std;

int main()
{
int n;

cout << "Enter a number: ";

cin >> n;

for (int a = 1; a <= n; a++)
{
for (int b = 1; b <= a; b++)
{
cout << "*" << " ";
}

cout << endl;
}
system("pause");
return 0;
}
for (int b = 1; b <= a+2; b++)wont work because that only adds 2 more stores. That does not I need it to look like a pyramid with the top star being in the center.
Then you need to write spaces in front of the *s. The first star at the top gets 1/2 the max width of the final row, which you can compute. The next one depends on how wide each row is, but you should be able to compute how many spaces you need to lead with on each row.

Give it a try, if you can't figure it out, ask again.

you need a fixed width font for it to look right. That will be fine in the console, but it may be off in anything else.
Last edited on
Topic archived. No new replies allowed.