Draw shapes (Triangle)
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:
- The program declares two variables
iandj, and initializes them to 1. - The program starts the outer for loop.
- The outer for loop checks if
iis less than or equal to 5. If it is, the inner for loop is executed. - The inner for loop checks if
jis less than or equal toi. If it is, the program prints an asterisk to the console. - The inner for loop then increments
jby 1. - The inner for loop repeats steps 4 and 5 until
jis greater thani. - After the inner for loop has terminated, the program prints a newline character to the console.
- The outer for loop then increments
iby 1. - The outer for loop repeats steps 3-8 until
iis 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:
- The program declares two variables
iandj, and initializes them to 5 and 1, respectively. - The program starts the outer for loop.
- The outer for loop checks if
iis greater than or equal to 1. If it is, the inner for loop is executed. - The inner for loop checks if
jis less than or equal toi. If it is, the program prints an asterisk to the console. - The inner for loop then increments
jby 1. - The inner for loop repeats steps 4 and 5 until
jis greater thani. - After the inner for loop has terminated, the program prints a newline character to the console.
- The outer for loop then decrements
iby 1. - The outer for loop repeats steps 3-8 until
iis 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:
- The program declares three variables
i,j, andk, and initializes them to 1, 4, and 1, respectively. - The program starts the outer for loop.
- The outer for loop checks if
iis less than or equal to 5. If it is, the inner for loop is executed. - The inner for loop checks if
jis greater than or equal toi. If it is, the program prints a space to the console. - The inner for loop then decrements
jby 1. - The inner for loop repeats steps 4 and 5 until
jis less thani. - After the inner for loop has terminated, the program prints an asterisk to the console for each iteration of the inner for loop.
- The program then prints a newline character to the console.
- The outer for loop then increments
iby 1. - The outer for loop repeats steps 3-9 until
iis 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:
- The program declares four variables
e,a, c, andb, and initializes them to 1, 1, 0 and 4, respectively. - The program starts the outer for loop.
- The outer for loop checks if
ais less than or equal to 5. If it is, the inner for loop is executed. - The inner for loop checks if
bis greater than or equal toa. If it is, the program prints a space to the console. - The inner for loop then decrements
bby 1. - The inner for loop repeats steps 4 and 5 until
bis less thana. - After the inner for loop has terminated, the program prints an asterisk to the console for each iteration of the inner for loop.
- The program then prints a newline character to the console.
- The outer for loop then increments
aby 1. - The outer for loop repeats steps 3-9 until
ais 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.