hello all,
i am trying to make a program using nested for loops that outputs a triangle made of *. so far i have part of the triangle, but am having diffuculty finishing it. my code so far:

#include<iostream> 
#include<conio.h>
using namespace std
void main()
{
      int n = 5;
      char usr = '*'; 
      int x;
      int y;    

       for(y=1;y<=n;y++)
           {
                 for(x=1;x<=y;x++)
                      {
                           cout<<usr;                                                 
                       }
                  cout<<endl;
            }
         getch();
}

it looks like this:
*
**
***
****
*****


but i want it to look like this:
*
**
***
****
*****
****
***
**
*

any help would be appreciated

Dani AI

Generated

The upward triangle in the original code is fine. The reason the second half printed as a single line of four stars is that the reverse pass you added only printed a star character repeatedly without producing one complete line per value — you need to print y stars then a newline for each decreased y. was right that you need a reversed outer pass; the missing piece in your attempt was an inner-per-line output (or an equivalent string-based approach).

A concise, modern C++ way that avoids an explicit inner loop is to use std::string to build each line and print it with a newline. For example:

#include <iostream>
#include <string>

int main() {
    const int n = 5;
    for (int i = 1; i <= n; ++i)
        std::cout << std::string(i, '*') << '\n';

    for (int i = n - 1; i >= 1; --i)
        std::cout << std::string(i, '*') << '\n';
}

Alternate approach: keep a single std::string, push_back('*') to grow and print each growing line, then pop_back() to shrink and print each shrinking line. That is memory-efficient and easy to reason about.

Quick troubleshooting notes: use int main() and return 0; (avoid nonstandard conio.h/getch), make sure loop counters are signed when you decrement them, and ensure you print a newline at the end of each line (cout << '\n' or cout << endl). These small fixes will produce the exact symmetric triangle you want.

Recommended Answers

All 3 Replies

Well, if you want to do it with loops, you just need to reverse your loop and you'll generate the other side of the pyramid. for (y=n-1;y>=1;y--) {...} should generate the other end.

i tried that loop i put it right befor getch();
i put:

for (y=n-1;y>=1;y--)
	{
		cout<<usr;
	}

and what i got was:
*
**
***
****
*****
****

Correct -- your first attempt was:

for(y=1;y<=n;y++)		// iterate over the number of lines you need to display
{
	for(x=1;x<=y;x++)	// outputs the desired number of '*'s
	{
		...
	}
}

I'm saying you need to reverse that process -- instead of counting 'up' lines, you need to count 'down' now, which is the outer loop I showed. You still need to provide another loop to output the right number of '*'s, though.

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.