Problem Solving: Printing Triangular Pattern
Let's create different triangular patterns using nested loops.
We'll cover the following
Printing a triangular pattern
In this lesson, we’ll create different triangular shapes. We’ll learn to identify the patterns within different shapes by making tables and then writing the conditions of the for
loops based on the observed patterns.
We’ll also be making functions to create each shape. So let’s begin!
Triangle shapes
Our goal is to print the following triangle shapes. Let’s look at each of these patterns one by one.
Height: 6
Symbol: *
* ****** ****** *
** ***** ***** **
*** **** **** ***
**** *** *** ****
***** ** ** *****
****** * * ******
(A) (B) (C) (D)
1. Triangle A
To print the first triangle, we will take symbol
and height
from the user and use a nested for
loop to print the following triangle.
Height: 6Symbol: **********************
Here, in the first line, there’s one asterisk (*
) printed. In the second line, there are two asterisks (**
) printed, and so on. 'Can you see a pattern here, in terms of ln
(line)?
Look at the table below to see the pattern.
Triangle A Pattern
Line # | Symbol (No. of Times) |
1 | 1 |
2 | 2 |
3 | 3 |
. | . |
. | . |
ln | ln |
The above pattern is really straightforward. Although we did not need to make the table,we did it anyway because it’s good practice as we’ll see when we go through the lesson.
Let’s translate the pattern above into nested loops.
- The outer
for
loop will be responsible for the total number of lines to be printed usingln
as a counter, starting from1
toheight
of the triangle.
for(int ln = 1; ln<=height; ln++){// Add code to print pattern for each line.cout << endl;}
-
The inner
for
loop will print the number of symbols for each line. The pattern suggests that we should print the symbolln
times for each line.- For example, in the first iteration (for
ln=1
) inner loop will print one asterisk (*
), - In the second iteration (for
ln = 2
), the inner loop will print two asterisks (**
)
- For example, in the first iteration (for
Therefore in general:
...//for all lines till the heightfor(int c = 1; c<=ln; c++) // print the pattern for each linecout << symbol;...
Look at the complete code below.
#include <iostream> using namespace std; void shapeA(char symbol, int height); int main() { int height; char symbol; cout << "Enter height: "; cin >> height; cout << "Enter symbol: "; cin >> symbol; cout<<'\n'; shapeA(symbol,height); return 0; } void shapeA(char symbol, int height) { for(int ln = 1; ln <= height; ln++) // line number 1 till height { for(int c = 1; c <= ln; c++) //In each line print symbol number of time { cout << symbol; // print * or any character } cout << endl; // moving to next line } }
- In line 13, we call the function
shapeA()
in which we pass the inputssymbol
andheight
. - In line 18, we have an outer
for
loop that ranges1
till theheight
of the pattern. In the outer loop, we haveendl
in line 24, which adds a new line. - In line 20, we have an inner
for
loop that printssymbol
in each iterationln
times.
2. Triangle B
Let’s look at our second pattern.
Height: 6Symbol: **********************
-
Again, we have to print as many lines as
height
( using the outerfor
loop). That should be easy. -
For each line, let’s try to analyze how the asterisk pattern is made.
-
IIn line 1, it should print number of symbols equal to the
height
variable (In the above example, it should print six asterisks******
). -
In line 2,
height-1
symbols (five asterisks*****
) should be printed, and so on.
-
Let us look at our table below for this pattern.
Triangle B Pattern
Line # | Symbol (No. of Times) |
1 | height - 0 |
2 | height - 1 |
3 | height - 2 |
. | . |
. | . |
ln | height - (ln-1) |
Based on the pattern above, try and write the code for this problem in the editor below.
#include <iostream> using namespace std; void shapeB(char symbol, int height); int main() { int height; char symbol; cout << "Enter height: "; cin >> height; cout << "Enter symbol: "; cin >> symbol; cout<<'\n'; shapeB(symbol,height); return 0; } void shapeB(char symbol, int height) { // Write your code here }
3. Triangle C
Let’s look at our third triangular pattern now.
Height: 6Symbol: **********************
How can we create the above triangle?
Think for a while before you proceed.
Hint: In each line, are we printing just one symbol?
We are actually printing spaces followed by asterisks in each line. In the above example:
- In the first line, we print zero spaces followed by six asterisks.
- In the second line, we print one space followed by five asterisks.
Can you see a pattern yet?
We’ve created a table to help understand the pattern.
Triangle C Pattern
Line # | Space | Symbol (No. of Times) |
1 | 0 | height - 0 |
2 | 1 | height - 1 |
3 | 2 | height - 2 |
. | . | . |
. | . | . |
ln | ln-1 | height - (ln-1) |
So our code’s structure would be as follows:
for(int ln=0; ln<=height; ln++){// 1. Printing spaces// Add code here to print spaces// 2. Printing symbols// Add code here to print symbolscout << endl; // The end of line}
Printing the spaces should follow the given “Space” column pattern in the following way:
for(int sp = 1; sp < ln; sp++)cout<<" ";
Printing the symbol should follow the given ”Symbol” column’s pattern in the following way:
for(int s = 1; s<= height-(ln-1); s++)cout<<symbol;
Let us write the complete code below:
#include <iostream> using namespace std; void shapeC(char sym, int H); int main() { char symbol; int lines; cout << "Enter height: "; cin >> height; cout << "Enter symbol: "; cin >> symbol; shapeC(symbol, lines); return 0; } void shapeC(char symbol, int height) { for(int ln=1; ln<=height; ln++)// line number 1 till height { // 1. Printing spaces for(int sp = 1; sp <= ln-1; sp++) { cout<<" "; } // 2. Printing symbols for(int a=1; a<=height-(ln-1); a++)// asterisks printing in each line { cout<<symbol; } cout << endl; // moving cursor to the next line } }
- In line 10, we call the function
shapeC()
in which we pass inputsymbol
andheight
as parameters. - In line 15, we have an outer
for
loop that ranges from1
till theheight
of the shape. In the outer loop, we haveendl
in line 25 that is used to move the cursor to the next line. - In lines 18–21, we have an inner
for
loop that prints spaces in each iteration. - In lines 23–26, we have an inner
for
loop that prints asymbol
in each iteration.
4. Triangle D
Let’s look at another example of a triangular pattern.
Height: 6Symbol: **********************
Again, we have to print symbols with spaces in each line.
- In the first line, we print five spaces with one asterisk.
- In the second line, we print four spaces with two asterisks.
It should be fairly straightforward by now.
Instruction: Think and figure out the pattern by making your own table on paper before looking at the table below.
Triangle D Pattern
Line # | Space | Symbol (No. of Times) |
1 | _________ | _________ |
2 | _________ | _________ |
3 | _________ | _________ |
. | . | . |
. | . | . |
ln | _________ | _________ |
Then, based on the pattern above, try and write the code for this one in the editor below.
#include<iostream> using namespace std; void shapeD(char symbol,int height); int main() { int height; char symbol; cout << "enter height: "; cin >> height; cout << "enter symbol: "; cin >> symbol; cout<<'\n'; shapeD(symbol,height); return 0; } void shapeD(char symbol,int height) { // write your code here }