0

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...

*Edited
by jemz*: n/a

This Question has been **Answered**

0

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.

0

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");
}
```

0

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.

0

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...

0

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.

0

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......

0

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...

This question has already been answered. Start a new discussion instead.

Recommended Topics

Hi there,

I want to implement undo logic which deletes the last drawn shape. To test this logic i had cleared shapes list and redrawn all shapes but still **last** ...

Hello eveyone,

Please tell me why triangle shape is redrawing on mouse move. It want triangle shape draw only once until mouse release.

Here is my code below. Thank you ...

I have the following query, to group the records but not the group was thanks to you only help, the end when running excel file view:

`SELECT TOP (100) PERCENT ...`