Draw shapes (Triangle)

Estimated reading: 6 minutes 105 Views

You can use the logic of nested loops to create different shapes, for example a triangle.

This code is a nested for loop that prints a triangle of asterisks to the console. The outer loop iterates 5 times, and the inner loop iterates for each iteration of the outer loop.

#include <iostream>

using namespace std;

int main()
{
    for ( size_t i = 1; i <= 5; i++)
    {
        for ( size_t j = 1; j <= i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

Here is a step-by-step explanation of what happens when you run this code:

  1. The program declares two variables i and j, and initializes them to 1.
  2. The program starts the outer for loop.
  3. The outer for loop checks if i is less than or equal to 5. If it is, the inner for loop is executed.
  4. The inner for loop checks if j is less than or equal to i. If it is, the program prints an asterisk to the console.
  5. The inner for loop then increments j by 1.
  6. The inner for loop repeats steps 4 and 5 until j is greater than i.
  7. After the inner for loop has terminated, the program prints a newline character to the console.
  8. The outer for loop then increments i by 1.
  9. The outer for loop repeats steps 3-8 until i is greater than 5.

Here is the output of the code:

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

What if we want to draw this triangle upside down?

We change the condition in our outer for loop.

This code is a nested for loop that prints an inverted triangle of asterisks to the console. The outer loop iterates from 5 to 1, inclusive, and the inner loop iterates for each iteration of the outer loop.

#include <iostream>

using namespace std;

int main()
{
    for ( size_t i = 5; i >= 1; i--)
    {
        for ( size_t j = 1; j <= i; j++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

Here is a step-by-step explanation of what happens when you run this code:

  1. The program declares two variables i and j, and initializes them to 5 and 1, respectively.
  2. The program starts the outer for loop.
  3. The outer for loop checks if i is greater than or equal to 1. If it is, the inner for loop is executed.
  4. The inner for loop checks if j is less than or equal to i. If it is, the program prints an asterisk to the console.
  5. The inner for loop then increments j by 1.
  6. The inner for loop repeats steps 4 and 5 until j is greater than i.
  7. After the inner for loop has terminated, the program prints a newline character to the console.
  8. The outer for loop then decrements i by 1.
  9. The outer for loop repeats steps 3-8 until i is less than 1.

Here is the output of the code:

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

Can we make it harder?

This code prints a right triangle of asterisks to the console. The outer loop iterates from 1 to 5, inclusive, and the inner loop iterates from 4 to i, inclusive, where i is the current iteration of the outer loop.

#include <iostream>

using namespace std;

int main()
{
    for ( size_t i = 1; i <= 5; i++)
    {
        for ( size_t j = 4; j >= i; j--)
        {
            cout << " ";
        }
        for (size_t k = 1; k <= i; k++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

Here is a step-by-step explanation of what happens when you run this code:

  1. The program declares three variables ij, and k, and initializes them to 1, 4, and 1, respectively.
  2. The program starts the outer for loop.
  3. The outer for loop checks if i is less than or equal to 5. If it is, the inner for loop is executed.
  4. The inner for loop checks if j is greater than or equal to i. If it is, the program prints a space to the console.
  5. The inner for loop then decrements j by 1.
  6. The inner for loop repeats steps 4 and 5 until j is less than i.
  7. After the inner for loop has terminated, the program prints an asterisk to the console for each iteration of the inner for loop.
  8. The program then prints a newline character to the console.
  9. The outer for loop then increments i by 1.
  10. The outer for loop repeats steps 3-9 until i is greater than 5.

Here is the output of the code:

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

Let’s print it upside down!

#include <iostream>

using namespace std;

int main()
{
    for ( size_t i = 5; i >= 1; i--)
    {
        for ( size_t j = 4; j >= i; j--)
        {
            cout << " ";
        }
        for (size_t k = 1; k <= i; k++)
        {
            cout << "*";
        }
        cout << endl;
    }

    return 0;
}

Look what we have here as an output!

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

Let’s draw a different triangle

#include <iostream>

using namespace std;

int main()
{
    int e = 1;
    for (int a = 1; a <= 5; a++)
    {
        for (int b = 4; b >= a; b--)
        {
            cout << " ";
        }
        for (int c = 0; c < e; c++)
        {
            cout << "*";
        }
        cout << endl;
        e = e + 2;
    }

    return 0;
}

Here is a step-by-step explanation of what happens when you run this code:

  1. The program declares four variables ea, c, and b, and initializes them to 1, 1, 0 and 4, respectively.
  2. The program starts the outer for loop.
  3. The outer for loop checks if a is less than or equal to 5. If it is, the inner for loop is executed.
  4. The inner for loop checks if b is greater than or equal to a. If it is, the program prints a space to the console.
  5. The inner for loop then decrements b by 1.
  6. The inner for loop repeats steps 4 and 5 until b is less than a.
  7. After the inner for loop has terminated, the program prints an asterisk to the console for each iteration of the inner for loop.
  8. The program then prints a newline character to the console.
  9. The outer for loop then increments a by 1.
  10. The outer for loop repeats steps 3-9 until a is greater than 5.

Here is the output of the code:

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

Let’s make it upside down:

#include <iostream>

using namespace std;

int main()
{
    int e = 9;
    for (int r = 1; r <= 5; r++)
    {
        for (int c = 0; c < e; c++)
        {
            cout << "*";
        }
        cout << endl;
        e = e - 2;
        for (int s = 0; s < r; s++)
        {
            cout << " ";
        }
    }

    return 0;
}

Output:

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

You can also use nested loops to draw other shapes, such as squares, rectangles, and diamonds.

Share this

Draw shapes (Triangle)

Or copy link

CONTENTS
English