# Printing Four Triangles in C with Asterisks

Page 1 of 1

## 3 Replies - 1831 Views - Last Post: 21 September 2011 - 10:11 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248037&amp;s=f9b20e07946b38ebb93332dce4f7d751&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Archael

Reputation: 0
• Posts: 2
• Joined: 21-September 11

# Printing Four Triangles in C with Asterisks

Posted 21 September 2011 - 09:40 AM

Hello, I am fairly new to C having just started a class for it earlier this month. This is a homework assignment, but I am certainly not asking for answers, merely some assistance or rather a nudge in the right direction, if you will.
What I am trying to do is print four triangles of asterisks, going in this order:

*
**
***

***
**
*

***
_**
__*

__*
_**
***

(EDIT: The spaces were being taken out when I posted it, so I added underscores so the placement of asterisks would be correct. I need the print to be spaces instead of underscores.)

This set up, except going up to ten asterisks. I also put spaces between the triangles here just so it can be seen clearer. What I have so far:

```#include <stdio.h>

int main (void)

{

int i,j;

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

{
int t,f;

t=1;
for (t=1; t<=10; t++) {
for (f=10; f>t-1; f--) {

printf("*");
}
printf("\n");
}

{
int u,h;

u=1;
for(u=1; t<=10

return 0;
}
}

```

Any help on how to get the spaces to show up before the asterisks in the second two triangle patterns would be greatly appreciated. Once again, I'm still fairly new and have a hard time understanding the logic of the code, but I am starting to grasp it slightly.

This post has been edited by Archael: 21 September 2011 - 09:46 AM

Is This A Good Question/Topic? 0

## Replies To: Printing Four Triangles in C with Asterisks

### #2 Flipside

Reputation: 12
• Posts: 70
• Joined: 03-July 09

## Re: Printing Four Triangles in C with Asterisks

Posted 21 September 2011 - 10:06 AM

I'm not sure how to explain this without providing the code, so here it is for one of the triangles:

```for(u=1; u <= 10; u++) {
for(h=1; h <= 10; h++) {
if(h < u) {
printf(" ");
}
else {
printf("*");
}
}
printf("\n");
}
```

So, 'u' controls the point at which you switch between spaces and asterisks. 'h' iterates through each line. As 'u' increases, each line has one more space and one less asterisk.

Also, just a side note, you don't need to set a value for a variable before the for loop, since you reset it within the for loop. For example, you don't need 'u=1;' here:

```u=1;
for(u=1; u <= 10; u++) {
```

```for(u=1; u <= 10; u++) {
```

### #3 Salem_c

• void main'ers are DOOMED

Reputation: 1418
• Posts: 2,681
• Joined: 30-May 10

## Re: Printing Four Triangles in C with Asterisks

Posted 21 September 2011 - 10:08 AM

You also need one of these in each loop as well.
```   for( j=1; j<i+1; j++) {
printf(" ");

```

Obviously the limit condition will have to change, otherwise you just print equal number of spaces and stars.

But with a bit of thought, you should be able to work out how many spaces each row has, given an i value.

### #4 Archael

Reputation: 0
• Posts: 2
• Joined: 21-September 11

## Re: Printing Four Triangles in C with Asterisks

Posted 21 September 2011 - 10:11 AM

Aha, I see. That was exactly the problem I was having just now while trying to tackle it while waiting for a response. I kept getting a single space at the start, and each row would print ten times. This has cleared up a lot for me. Also, to ensure I learn it better, and to better stay within the rules of the forum, I'll use the help you two provided to write the code myself.

Thanks a bunch!