Take the 2-minute tour ×

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;

int blank=size/2;
int newsize=1;
for (int i=0; i<=size/2; i++)
    for(int j=blank;j>0;j--)
        cout <<" ";
    for(int j=newsize; j>0; j--)
        cout <<"*";
    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 :)

share|improve this question
by straight i mean aligned, i just couldnt fix the triangle to look like that –  carla Dec 13 '10 at 2:33
Do you mean aligned to right? You can't align it to be a pyramid with 1,2,3 but yes with 1,3,5... –  Eric Fortis Dec 13 '10 at 2:55
add comment

3 Answers

up vote 0 down vote accepted

I'm not sure what you mean by "but straight", so I'll just ignore that for now...

Start with blank the same value as size, so that you can decrement the value each time without having to decrement by a half:

int blank=size;

Loop up to size instead of size/2 to get more lines:


Decrement by two in the loop for spaces to get half the number of spaces:

for(int j=blank;j>0;j-=2)

Increase the size by one instead of two to get the slower increase:


That should produce the output that you showed.


I tested this to be sure, and the output is:


To get the exact output that you asked for, start with blank one less:

int blank=size - 1;
share|improve this answer
you know you were right!!! thank you!!! :) –  carla Dec 13 '10 at 2:35
actually this gave me another pattern :O –  carla Dec 13 '10 at 2:41
add comment

Did I get it right: you want to place some asterisks on borders of character places? If so, it isn't possible. Every asterisk (or any other symbol), when displayed in monospace fonts, will reside in a middle of a character place, like in a grid. You can place asterisks inside the cells, but you cannot place asterisks on the borders of the grid.

share|improve this answer
no i just want to make the triangle pattern; it is possible, i just cant get it exactly correct –  carla Dec 13 '10 at 2:31
uhm, could you please draw the pattern, to make it possible to understand what you want exactly? –  ulidtko Dec 13 '10 at 2:34
the pattern is the first triangle :) –  carla Dec 13 '10 at 2:35
huh, I thought you want to align the first pattern to look really straight :) –  ulidtko Dec 13 '10 at 2:45
sorry, i meant aligned because it doesnt look like a triangle :) but my only problem is the number of stars shown. –  carla Dec 13 '10 at 2:47
add comment
int NUMLINES = 5;

void display(int, char);

void main(){
for (int i=1; i<= NUMLINES; ++i){
    display((NUMLINES + 1 - i), ' ');
    display(( 2 * i - 1 ), '*');
    cout << endl;

void display (int howmany, char symbol){
for (int i = 1; i<=howmany; ++i)
 cout << symbol;
share|improve this answer
add comment

Your Answer


