# Triangle

Page 1 of 1

## 2 Replies - 586 Views - Last Post: 18 February 2014 - 08:17 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=340299&amp;s=2c43d271731f285d6eb15a438f2cf753&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Karia

Reputation: 0
• Posts: 1
• Joined: 18-February 14

# Triangle

Posted 18 February 2014 - 05:12 AM

Help me to make a program to make a triangle like given below: with for loop...

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

I tried like this...

```for i in range(1,6):
for j in range(1,i):
print "*",
print
```

the out put is
```*
* *
* * *
* * * *

```

This post has been edited by Atli: 18 February 2014 - 05:34 AM
Reason for edit:: Use [code] tags when posting code.

Is This A Good Question/Topic? 0

## Replies To: Triangle

### #2 Atli

• D.I.C Lover

Reputation: 3811
• Posts: 6,224
• Joined: 08-June 10

## Re: Triangle

Posted 18 February 2014 - 05:59 AM

You skipped the most important step of the programming process: understand the logic yourself! Before you even touch a keyboard, you need to first understand how to solve the problem; what logic is required to accomplish the task. - Have you figured out how to do that yet?

### #3 baavgai

• Dreaming Coder

Reputation: 6084
• Posts: 13,176
• Joined: 16-October 07

## Re: Triangle

Posted 18 February 2014 - 08:17 AM

Your problem is really just a row for number problem. That is, for a given row, what is the number of stars.

What you have may be rewritten as:
```for i in range(1,6):
print i, '*' * i

```

Results:
```1 *
2 **
3 ***
4 ****
5 *****

```

However, the 1 to 5 thing is unnecessary. You could instead figure out the value you want for a given row. e.g.
```for i in range(5):
ct = i + 1
print i, ct, '*' * ct

```

Results:
```0 1 *
1 2 **
2 3 ***
3 4 ****
4 5 *****

```

Thinking of it like this, your solution becomes:
```PEEK=5
SIZE=PEEK + PEEK - 1

for i in range(SIZE):
ct = # your code here
print i, ct, '*' * ct

```

Results:
```0 1 *
1 2 **
2 3 ***
3 4 ****
4 5 *****
5 4 ****
6 3 ***
7 2 **
8 1 *

```

Good luck.