hello can you help me how can i print asterisk to form triangle like this

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

here is my code

int main()
 {
   for(i=0;i<10;i++)
    for(j=0;j<i;j++)
     {
      printf("*");
     }
      printf("\n");

     getch()
     return 0;
}

please help me hoping for your positive responds...thank you in advance...

Recommended Answers

All 7 Replies

You're not going to call your new line with that for loop.

for(i=0;i<10;i++)
  {
    for(j=0;j<i;j++)
    {
      printf("*");
    }
    printf("\n");
  }

Will now give the output:

*
**
***
****
...

So you now need to change the logic to add some spaces to make your * centre. I'll leave that one with you, once you have some updated code if you need any more help post back.

You're not going to call your new line with that for loop.

for(i=0;i<10;i++)
  {
    for(j=0;j<i;j++)
    {
      printf("*");
    }
    printf("\n");
  }

Will now give the output:

So you now need to change the logic to add some spaces to make your * centre. I'll leave that one with you, once you have some updated code if you need any more help post back.

hello sir here's my updated code i just added "\t" so that i can center my * ..sir what should i do now do i need to put another forloop..please help me sir hoping for your positive responds..

for(i=0;i<10;i++)
  {
    printf("\t");
    for(j=0;j<i;j++)
    {
      printf("*");
    }
    printf("\n");
  }

As I said, you need to add some formatting to cnetralise the * in each row. Until you have some code to show how you've attempted that I can't really help you.

As I said, you need to add some formatting to cnetralise the * in each row. Until you have some code to show how you've attempted that I can't really help you.

hello sir,

can you give me snippet code for formmatting,i don't have idea on this sir...thank you in advance hoping for your positive responds...

To find the center column for the first *, you can use either the number of rows in the "tree", or the number of *s, in the bottom row.

So if the bottom row has 7 *s, then 7/2 equals 3. So you should have 3 spaces before the * on the first row. You can use a for loop (outer one), to code this:

//handles the outer row configuration
for(numstars=1;numstars<=botmRow;numstars+=2) {
  numspaces = (botmRow - numstars)/2;

  //handles the spaces before the stars
  for(i=0;i<numspaces;i++)
    print one space here

  //prints out the star/s
  for(i=0;i<numstars;i++)
    print out star/s here
}

So the top row could be seen as:

spc spc spc * spc spc spc

because the top row will always have just ONE star. So all the other char's will have to be spaces. :)

Now for each row, the number of stars increases by 2, and that means the number of spaces on each side of the stars, will decrease by 1 (since there are two sides, and 2 * 1 = 2.

imo you don't want to use tabs for this assignment.

To find the center column for the first *, you can use either the number of rows in the "tree", or the number of *s, in the bottom row.

So if the bottom row has 7 *s, then 7/2 equals 3. So you should have 3 spaces before the * on the first row. You can use a for loop (outer one), to code this:

//handles the outer row configuration
for(numstars=1;numstars<=botmRow;numstars+=2) {
  numspaces = (botmRow - numstars)/2;

  //handles the spaces before the stars
  for(i=0;i<numspaces;i++)
    print one space here

  //prints out the star/s
  for(i=0;i<numstars;i++)
    print out star/s here
}

So the top row could be seen as:

spc spc spc * spc spc spc

because the top row will always have just ONE star. So all the other char's will have to be spaces. :)

Now for each row, the number of stars increases by 2, and that means the number of spaces on each side of the stars, will decrease by 1 (since there are two sides, and 2 * 1 = 2.

imo you don't want to use tabs for this assignment.

hello sir thank you for helping me sir it works....more power to you......

To find the center column for the first *, you can use either the number of rows in the "tree", or the number of *s, in the bottom row.

So if the bottom row has 7 *s, then 7/2 equals 3. So you should have 3 spaces before the * on the first row. You can use a for loop (outer one), to code this:

//handles the outer row configuration
for(numstars=1;numstars<=botmRow;numstars+=2) {
  numspaces = (botmRow - numstars)/2;

  //handles the spaces before the stars
  for(i=0;i<numspaces;i++)
    print one space here

  //prints out the star/s
  for(i=0;i<numstars;i++)
    print out star/s here
}

So the top row could be seen as:

spc spc spc * spc spc spc

because the top row will always have just ONE star. So all the other char's will have to be spaces. :)

Now for each row, the number of stars increases by 2, and that means the number of spaces on each side of the stars, will decrease by 1 (since there are two sides, and 2 * 1 = 2.

imo you don't want to use tabs for this assignment.

hello sir thank you for helping me...it works...more power to you...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.