# problem creating a star(*) triangle with for loop

Page 1 of 1

## 6 Replies - 69223 Views - Last Post: 05 January 2008 - 07:52 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=37129&amp;s=3fa1a1a5c268b4e92bfee344067c1732&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fgg_86

Reputation: 0
• Posts: 3
• Joined: 09-November 07

# problem creating a star(*) triangle with for loop

Posted 09 November 2007 - 11:17 AM

I am having trouble with using a for loop to create a triangle. The program displays a triangle of stars(*) based on how many rows the user enters. The rows entered must be between 3 and 20 and the actual triangle needs to have correct space formatting.

Basically what I have done for this program is created a nested for loop that lets the user enter the number of rows, then I use a for loop to start the triangle design like so:

for (int rows = 1; rows <= numRows; rows++)

(numRows is how many rows entered by user)

The problem I am having relates to what kind of for loop I need to nest in the 'rows' for loop. I can't decide if I need to make a for loop for the number of stars in each or make a for loop to show how many spaces display before the stars. If I had to guess I think I might need to make a loop for the number of stars then use formatting code like setw() to get the correct number of spaces.

Any suggestions?

Is This A Good Question/Topic? 0

## Replies To: problem creating a star(*) triangle with for loop

### #2 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: problem creating a star(*) triangle with for loop

Posted 09 November 2007 - 11:23 AM

well post the code you have so far in code tags like this:

welcome to </dream.in.code>
and try to search this forum.. you'll find many many threads similar like this one

### #3 fgg_86

Reputation: 0
• Posts: 3
• Joined: 09-November 07

## Re: problem creating a star(*) triangle with for loop

Posted 09 November 2007 - 11:33 AM

Alright it looks like this so far:

```#include <iostream>
using namespace std;

int main()
{
int numRows;
int spaces;

cout << "Enter an interger between 3 and 20: ";
cin >> numRows;
cout << endl;

for (int rows = 1; rows <= numRows; rows++)
```

### #4 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: problem creating a star(*) triangle with for loop

Posted 09 November 2007 - 03:15 PM

Ok the big hint is using another loop, so you are basically using a nested for loop where the limit in the condition of the inner loop would the iterator of the outer loop. See where I'm getting at? So the output should be:
user eneters number of rows say.. 5
the triangle will look like
*
**
***
****
*****

a nested for loop looks like this:
```for(;;)
{
for(;;)
{
//something
}
//something
}
```

This post has been edited by PennyBoki: 09 November 2007 - 03:16 PM

### #5 fgg_86

Reputation: 0
• Posts: 3
• Joined: 09-November 07

## Re: problem creating a star(*) triangle with for loop

Posted 12 November 2007 - 01:46 PM

Right I know that I need a nested for loop, maybe even a nested loop inside the first nested for loop, my problem is that I can't figure out the syntax behind it.

Like I said I know that I need a nested loop, however I can't see how to type the syntax to create this triangle formatted with setw():

* <-----3 spaces before *
***<-----2 spaces before *
*****<-----1 space before *
*******<-----0 spaces before *

I DO know that the nested for loop is necessary to determine the rows and stars. The outside loop is for rows and the inside loop is for determining the stars in each row. With that in place I could use setw() to make the correct number of spaces in each row.

If I had to guess on how to enter this I would say:

```for (int rows = 1; rows <= numRows; rows++)
{
for (int i = 1; i <=
```

Right there at the end is where I draw a blank

Any thoughts anyone?

### #9 aaron.henriques

Reputation: 2
• Posts: 20
• Joined: 20-September 07

## Re: problem creating a star(*) triangle with for loop

Posted 05 January 2008 - 06:06 AM

```#include<iostream.h>
#include<conio.h>
void main()
{	   clrscr();
int n;
cout<<"enter any number of rows";
cin>>n;
for(int i=1;i<=n;i++)
{	   for(int j=1;j<=i;j++)
cout<<"*";
cout<<"\n";
}
}

```

Try this program it will work...

### #10 PennyBoki

• D.I.C Lover

Reputation: 55
• Posts: 2,345
• Joined: 11-December 06

## Re: problem creating a star(*) triangle with for loop

Posted 05 January 2008 - 07:52 AM

Hi aaron.henriques, that is nice of you to help but I suggest you read the forum rules. The member needs to show effort in code so that someone could help.

PS your code is not standard-compliant and there is no need to open threads that are well out of date.

This post has been edited by PennyBoki: 05 January 2008 - 07:54 AM