I need t o make these paterns

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

I can make only the first one, this is what I have, please help make the other ones in the same program

for (int r=1; r<=10; r++)
	{
		for (int c=1; c<=r; c++)
			cout<<"*";
		cout<<endl;
	}

Dani AI

Generated

The picture shows four right triangles side by side: one growing left-aligned, one growing right-aligned, one shrinking right-aligned, and one shrinking left-aligned. was right about using a fixed‑width font to check spacing, and correctly pointed out that the inner loop must sometimes print blanks then stars (the space count is a simple function of the outer loop). The easiest, clearest approach is to compute the number of stars for each block and use the std::string constructor to build those chunks rather than juggling nested loops for every single blank.

This small C++ example prints the four triangles on one line per row (change n to scale). It uses string(count, char) to produce runs of spaces or stars and shows the relationships you need: right-justified blocks need n - r leading spaces; decreasing blocks use n - r + 1 stars and r - 1 leading spaces.

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

int main() {
    const int n = 10;
    const string sep = "   "; // adjust spacing between triangles
    for (int r = 1; r <= n; ++r) {
        cout << string(r, '*') << sep;                       // left-increasing
        cout << string(n - r, ' ') << string(r, '*') << sep; // right-increasing
        cout << string(r - 1, ' ') << string(n - r + 1, '*') << sep; // right-decreasing
        cout << string(n - r + 1, '*') << '\n';              // left-decreasing
    }
    return 0;
}

Troubleshooting notes: compile with a C++11 or later compiler and include <string>. If alignment looks off, increase sep or print each block with a fixed column width. Avoid off‑by‑one errors by verifying the formulas n - r, r - 1, and n - r + 1 against the first and last rows. If you prefer iomanip, ’s setw approach works too, but string construction keeps the logic explicit and easy to debug.

Recommended Answers

All 13 Replies

they should be triangles with 1 star going to 10 and 1 reow to 10

Sorry, but you're going to have to fix the formatting of your triangles. I added code tags to preserve the spacing, but it looks like you used Daniweb's input box to format everything. The input box doesn't use a fixed width font type, so what looks good when you type it looks like crap when it's submitted. Use a text editor with a fixed width font to get your triangles right, then paste them into code tags.

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

is that better

Well, that's the same thing that you posted before. However, it's not a common variation of the "I want to make a triangle" crowd, which is why I suggested that the forum is messing up your careful formatting.

Assuming that triangles 2-4 are just reflections/rotations of the first one - all being right triangles of size 10 on the perpendicular sides, all you need are variations of the program you have.

You may have to make the inner loop decrement instead of increment. You may have to display blank spaces for some number of iterations, then stars. Look for relationships between the inner and outer loop.

triangles 2-4 are just reflections/rotations of the first one

I have 2 but can't get the other two. This is what I have.

/*	for (int r=1; r<=10; r++)
	{
		for (int c=1; c<=r; c++)
			cout<<"*";
		cout<<endl;
	}

 */
/*
	for (int r=10; r>=1; r--)
	{
		for (int c=1; c<=r; c++)
			cout<<"*";
		cout<<endl;
	}
*/	//or both do the same
	for (int r=1; r<=10; r++)
	{
		for (int c=10; c>=r; c--)
			cout<<"*";
		cout<<endl;

I changed the variables so they would run on one program
This is waht I cganged them to

for (int a=1; a<=10; a++)
	{
		for (int b=1; b<=a; b++)
			cout<<"*";
		cout<<endl;
	}
	cout<<endl;
 
	for (int c=10; c>=1; c--)
	{
		for (int d=1; d<=c; d++)
			cout<<"*";
		cout<<endl;
	}
	cout<<endl;
/*//or
	for (int e=1; e<=10; e++)
	{
		for (int f=10; f>=e; f--)
			cout<<"*";
		cout<<endl;
	}
	cout<<endl;

bbcode

my teatcher says I need to add a for loop to do the other 2, but I don't know what to do. Please help me

For the other two, the body of your inner loop will be a bit more busy. Either have another loop that prints the blank spaces, based on a relationship between the inner and outer loop variables, or use an if statement to determine if you display blanks or stars.

Also, you don't need to use different variables for each loop set. If you declare the variables before the loops, just keep reusing them.

int a, b;

for( a = 0; i < 10; a++ )
   for( b = 0; b < 10; b++ )
      //do something

for( a = 0; i < 10; a++ )
   for( b = 0; b < 10; b++ )
      //do something else

Even with the style you're using, declaring the counter variables in the loop statements, you could reuse the names a and b.

timdog i'm in the same position you are. I am able to make the first two of your traingles and after that I'm having a hard time re writing the loop.

someone in my class told me that it should look like this

for (r=10; r>=1; r--)
{
for (c=1; c<=r; c++)
		
for(int i = 0; i < 15; i++) 
cout << ' '; 
cout << '*';  
cout<<endl;
}
cout<<endl;

but that does not work, whats wrong, please help.

let the c and i loops be separate, inside the r loop.

i should come first, and be limited by the current value of r - how many blanks need to be displayed for the current line. c should pick up at the value i left off, limited by length of a row.

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

int main()
{   
       for (int r=1,int space=25,int s=2; r<=10; r++,space-=2,s+=2)
    {

    for (int c=1; c<=r; c++)
        cout<<"*";


    cout<<setw(space-1);


        for(int j=1;j<=r;j++)
        cout<<"*";


    cout<<setw(15);     

    for (int c=10; c>=r; c--)
        cout<<"*";

    cout<<setw(s); 

    for (int c=10; c>=r; c--)
    cout<<"*";


    cout<<endl;
    }

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