Draw an outline of an upward-pointing triangle I am beginner in C++ and I was given this assignment by my teacher.  The assignment is to print the following shape:

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


The following is what I have tried and basically it works, but I doubt my code is not simple, there's another way simpler than that. How can I make my code more simpler? Any hints are appreciated.

#include &lt;iostream&gt;

using namespace std;

int main() {
  /**
      *
     * *
    *   *
   *     *
  *       *
 *         *
*************
  */
  int spaces_before = 6; // spaces before printing the stars
  int spaces_after = 0; // spaces after printing the first star
  int stars = 1;
  for (int i = 1; i &lt;= 7; i++) { 
    for (int j = 1; j &lt;= spaces_before; j++) {
      cout &lt;&lt; ' ';
    }
    if (i == 1) {
      cout &lt;&lt; "*";
    } else if (i != 7) {
      cout &lt;&lt; '*';
      for (int j = 1; j &lt;= spaces_after; j++) {
        cout &lt;&lt; ' ';
      }
      cout &lt;&lt; '*';
    } else {
      for (int j = 1; j &lt;= stars; j++) {
        cout &lt;&lt; '*';
      }
    }
    spaces_before--;
    if (i == 1) {
      spaces_after++;
    } else {
      spaces_after += 2;
    }
    cout &lt;&lt; '\n';
    stars += 2; 
  }
}
First of all, please avoid using namespace std;.  It is considered bad practice and will cause many problems.  See Why is using namespace std; considered bad practice? for more information.

Now let's try to simplify the logic.  The following loop:

for (int j = 1; j &lt;= spaces_before; j++) {
  cout &lt;&lt; ' ';
}


is a very verbose way to write

std::cout &lt;&lt; std::string(spaces_before, ' ');


(this happened more than one time.)

And you don't need to keep the variables spaces_before, spaces_after, and star.  Just calculate them on demand.  Also, don't include the special cases in the loop:

#include &lt;iostream&gt;
#include &lt;string&gt;

int main()
{
    std::cout &lt;&lt; "      *\n";
    for (int i = 0; i &lt; 5; ++i)
        std::cout &lt;&lt; std::string(5 - i, ' ') &lt;&lt; '*'
                  &lt;&lt; std::string(2 * i + 1, ' ') &lt;&lt; "*\n";
    std::cout &lt;&lt; std::string(13, '*') &lt;&lt; '\n';
}


This should be enough since you are a beginner.  After this is done, it would be very nice if you extract the magic numbers into named constants.  And the whole process can be wrapped in a function for reuse:

#include &lt;iostream&gt;
#include &lt;string&gt;

void print(std::ostream&amp; os, int rows)
{
    os &lt;&lt; std::string(rows - 1, ' ') &lt;&lt; "*\n";
    for (int i = 1; i &lt; rows - 1; ++i)
        os &lt;&lt; std::string(rows - i - 1, ' ') &lt;&lt; '*'
                  &lt;&lt; std::string(2 * i - 1, ' ') &lt;&lt; "*\n";
    os &lt;&lt; std::string(2 * rows - 1, '*') &lt;&lt; '\n';
}

int main()
{
    print(std::cout, 7);
}