how could i make a triangle thatgot spaces/hole at it's centre.. :

for example like this :

---------------- *
--------------*----*
------------*--------*
----------*------------*
---------***********

-->> which the "------" is actually the space...

i've tried working with this code but its not working :( :

#include <iostream>
using namespace std;

int main ()
{
    int a,b;
    
    for (a=1; a<=10; a++)
    {
        for (b=1; b<=10-a; b++){
            cout<<" ";
            }
            for (b=1; b<=2*a-1; b++){
                
                if(b%2==0){
                cout<<"*";
                }else cout<<" ";
                
                }
                cout<<endl;
    }
system("pause");
}

what changes do i need to make this code working like what i desired??


Thank You for helping....

Dani AI

Generated

This thread is asking for a hollow isosceles triangle (edges starred, interior spaces, base filled) rather than the full solid triangle. showed the desired output; demonstrated the trivial hard-code approach; correctly recommends meaningful names; and pointed to the key idea: print the edge stars and make the last row solid. The simplest, robust algorithm works row-by-row and avoids parity tricks.

Algorithm (brief): for height n and row i (1..n) compute leading = n - i and width = 2i - 1. Print leading spaces. If i == 1 print a single `. If i == n printwidthstars. Otherwise print, thenwidth-2spaces, then`.

Example C++ implementation (change height or read it from input):

#include <iostream>

int main() {
    int height = 5;             // set or read from std::cin
    for (int row = 1; row <= height; ++row) {
        int leading = height - row;
        int width   = 2 * row - 1;

        for (int s = 0; s < leading; ++s) std::cout << ' ';

        if (row == 1) {
            std::cout << '*' << '\n';
        } else if (row == height) {
            for (int k = 0; k < width; ++k) std::cout << '*';
            std::cout << '\n';
        } else {
            std::cout << '*';
            for (int i = 0; i < width - 2; ++i) std::cout << ' ';
            std::cout << '*' << '\n';
        }
    }
    return 0;
}

Troubleshooting/cautions: watch off-by-one errors (width = 2row-1), test small heights (1 and 2), avoid platform-specific pauses (no system("pause")), and trim trailing spaces if exact formatting matters. To generalize, read height from input and replace `''` with any fill character.

Recommended Answers

All 3 Replies

This won't solve your problem I guess. But this was my job interview question.. Here is what I proposed:

cout << "---------------- *" << endl;
cout << "--------------*----*" << endl;
cout << "------------*--------*" << endl;
cout << "----------*------------*" << endl;
cout << "---------***********" << endl;

Of course I didn't get the job.. :D

Create two meaningful variable names, like

int spacesBeforeFirstStar = 11;
int numSpacesBetweenStars = 0;

Then perhaps

for ( rows = 0 ; rows < 5 ; rows++ ) {
  // some stuff
  spacesBeforeFirstStar -= 2;
}

Not meaningless variable names like a and b.

Are you trying something like this?

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

If possible, try to generalize the program for a triangle of order n (where n could be the height).
One method of solving this could be for you to first develop a program for a triangle without spaces like this one

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

After that, you could modify the program to print stars only during starting and ending of loops, otherwise spaces. However, you should keep it in mind to print the entire line of stars in the last iteration for the base of the triangle.

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.