Answer to Question #94608 in C++ for b

Question #94608
I need to function to draw an equilateral triangle in C++. with # as the border and '*' as the filler after accepting the size from the user.
1
Expert's answer
2019-09-16T04:38:01-0400

#include <iostream>

using namespace std;


int main()

{

  int height;

  cout << "Enter height: ";

  cin >> height;


  // length of the line to be filled with '*'

  int z=1;


  // Display all lines except the bottom one

  for (int i=0; i<height-1; i++) {

    // spaces before the border

    for (int j=height-1; j>i; j--)

      cout << " ";

    // ... and border itself

    cout << "#";


    // On the first row (i==0), there is no filling

    if(i!=0) {

      //

      for (int k=1; k<=z; k++)

        cout << "*";

      cout << "#";

      z += 2;

    }

    cout << endl;

  }


  // The bottom line consists of '#' only

  for (int i=0; i<=z+1; i++)

    cout << "#";


  cout << endl;


  return 0;

}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS