Shapes Printing Solution Review: Numbers/Alphabets Triangles
Learn to print different shapes of triangles.
We'll cover the following
In this lesson, we will use nested loops to print different shapes of triangles with alphabets and numbers.
Printing triangle with numbers
Challenge 1: Upright Right Triangle
Let’s start with the first triangle in which we are printing numbers in each line according to the line number.
h = 5 h = 7 h = 9
1 1 1
1 2 1 2 1 2
1 2 3 1 2 3 1 2 3
1 2 3 4 1 2 3 4 1 2 3 4
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 6 1 2 3 4 5 6
1 2 3 4 5 6 7 1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
Solution
Let’s figure out the pattern first:
Upright right triangle
Line # | Numbers in each line |
1 | (1) |
2 | (1 2) |
3 | (1 2 3) |
4 | (1 2 3 4) |
5 | (1 2 3 4 5) |
... | ... |
ln | (1 2 3 . . . ln) |
Let’s write the code following the above pattern.
#include <iostream>using namespace std;void printT1(int h){for(int ln=1;ln<=h;ln++){// it will print numbers in each line till lnfor(int i=1; i<=ln; i++)cout<<i<<" "; // to print numbers with spaces from 1 till ncout<<endl; // move to the next line}}int main(){printT1(9);}
We have given two implementations.
Nested loop: For each line ln
, we printed 1
up to ln
numbers. Lines 9–10 do that for us.
Function call: The loop is replaced by a function call, printUntillN()
, that prints numbers up to a limit n
. For each line, we have called that function to print up to ln
numbers.
Challenge 2: Upside Down Right Triangle
Let’s move to the second triangle:
h = 5 h = 7 h = 9
1 2 3 4 5 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9
1 2 3 4 1 2 3 4 5 6 1 2 3 4 5 6 7 8
1 2 3 1 2 3 4 5 1 2 3 4 5 6 7
1 2 1 2 3 4 1 2 3 4 5 6
1 1 2 3 1 2 3 4 5
1 2 1 2 3 4
1 1 2 3
1 2
1
Solution
Here’s the pattern.
Upside down right triangle
Line # | Numbers in each line |
1 | (h) numbers: (1 2 3 4 ... h) |
2 | (h - 1) numbers: (1 2 3 4 ... h-1) |
3 | (h - 2) numbers: (1 2 3 4 ... h-2) |
4 | (h - 3) numbers: (1 2 3 4 ... h-3) |
5 | (h - 4) numbers: (1 2 3 4 ... h-4) |
... | ... |
ln | (h - ln + 1) numbers |
#include <iostream>using namespace std;void printT2(int h){for(int ln=1;ln<=h;ln++){// it will print numbers in each line till h-ln+1for(int i=1; i<=h-ln+1; i++)cout<<i<<" "; // To print numbers with spaces from 1 till h-ln+1cout<<endl; // move to the next line}}int main(){printT2(7);}
Again, we have given two implementations, one using the nested loop and the other using printUntillN()
function.
Let’s move to the next challenge and its solution.
Challenge 3: Upright right triangle (with incremental values)
Let’s move to the third triangle:
h = 5 h = 7 h = 9
1 1 1
2 3 2 3 2 3
4 5 6 4 5 6 4 5 6
7 8 9 10 7 8 9 10 7 8 9 10
11 12 13 14 15 11 12 13 14 15 11 12 13 14 15
16 17 18 19 20 21 16 17 18 19 20 21
22 23 24 25 26 27 28 22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
Solution
Let’s look at the pattern first:
Upright Right Triangle
Line # | Numbers in each line |
1 | (1) |
2 | (2 3) |
3 | (4 5 6) |
4 | (7 8 9 10) |
5 | (11 12 13 14 15) |
... | ... |
ln | ln numbers |
Look at the pattern. The numbers that are being printed are continuously increasing. That can be achieved by making a variable number
outside the nested loop and after printing the number
incrementing it by one.
Here's the implementation.
#include <iostream>using namespace std;void printT3(int h){int number = 1;for (int ln = 1; ln <= h; ln++){for (int symb = 1; symb <= ln; symb++){cout << number << " ";number++;}cout << endl;}}int main(){printT3(7);}
-
In line 5:, we initialize the
number
with1
. -
In line 10:, we print the
number
with spaces in each line. -
In line 11:, we increment in
number
by1
-
In line 13:, we move the cursor to the next line after each iteration.
Let’s move to the next challenge and its solution.
Challenge 4: Palindromic Triangle of Numbers
Let’s move to the fourth triangle:
h = 5 h = 4
1 1
2 3 2 2 3 2
3 4 5 4 3 3 4 5 4 3
4 5 6 7 6 5 4 4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Solution
Let us try to figure out the pattern first:
Palindromic triangle of numbers
Line # | Spaces | Numbers in each line |
1 | (h - 1) * 2 | (1) followed by () |
2 | (h - 2) * 2 | (2 3) followed by (2) |
3 | (h - 3) * 2 | (3 4 5) followed by (4 3) |
4 | (h - 4) * 2 | (4 5 6 7) followed by (6 5 4) |
5 | (h - 5) * 2 | (5 6 7 8 9) followed by (8 7 6 5) |
... | ... | ... |
ln | (h - ln) * 2 | "ln" consecutive number starting from ln followed by "ln-1" consecutive number in decrement order starting from 1 less than where the above number stopped |
#include <iostream>using namespace std;void printT4(int h){for (int ln = 1; ln <= h; ln++){int Number = ln;for (int sp = 1; sp <= (h - ln); sp++)cout << " ";for (int t = 1; t <= ln; t++){cout << Number<<" ";Number++;}Number--; // decrementing it because Number has increased// one more than the last printfor (int t = 1; t < ln; t++) // will run ln-1 times{Number--; // Number should be decremented firstcout << Number<<" "; // and then gets printed}cout << endl; // move the cursor to the next line}}int main(){printT4(5);}
We have divided the above code into four parts:
- In lines 8-9:, we print the spaces in each line before number printing.
- In lines 11-15:, we print the numbers with spaces in ascending order.
- In lines 17-22:, we print the numbers with spaces in descending order.
- In line 24:, we move the cursor to the next line.
Printing triangle with alphabets
Challenge 5: Upright right triangle of letters 1
The program will print the following triangle:
h = 5 h = 6
A A
B B B B
C C C C C C
D D D D D D D D
E E E E E E E E E E
F F F F F F
Solution
Let’s figure out the pattern:
Upright Right Triangle of Letters 1
Line # | Alpha |
1 | 1st letter 1 time: (A) |
2 | 2nd letter 2 times: (B B) |
3 | 3rd letter 3 times: (C C C) |
4 | 4th letter 4 times: (D D D D) |
5 | 5th letter 5 times: (E E E E E) |
... | ... |
n | nth letter n times |
#include <iostream>using namespace std;void printT5(int h){char alpha = 'A';for (int line = 1; line <= h; line++){for (int symb = 1; symb <= line; symb++){cout << alpha << " "; // print alphabets till size of line}cout << endl; // move to the next linealpha++; // increment in alphabet by 1}}int main(){printT5(5);}
We printed the alphabet according to the line number in each line of the triangle. In the first line, we printed A
one time. In the second line, we printed B
two times. So the same sequence follows until the height of the triangle. After each line, we have incremented alpha
with 1
.
- In line 6:, we assign
alpha
toA
. - In line 11:, we print the alphabet with spaces.
- In line 14:, we move the cursor to the next line.
- In line 15, we increment in the alphabet in each iteration.
Challenge 6: Upright right triangle of letters 2
The program will print the following triangle:
h = 5 h = 6
A A
A B A B
A B C A B C
A B C D A B C D
A B C D E A B C D E
A B C D E F
Solution
Let’s figure out the pattern:
Upright Right Triangle of Letters 2
Line # | Alpha |
1 | (A) |
2 | (A B) |
3 | (A B C) |
4 | (A B C D) |
5 | (A B C D E) |
... | ... |
n | (A B C to nth letter) |
#include <iostream>using namespace std;void printT6(int h){for (int line = 1; line <= h; line++){char alpha = 'A';for (int t = 0; t < line; t++){cout << char(alpha+t) << " ";}cout << endl; // move to the next line}}int main(){printT6(5);}
Again, we can have two implementations, one with a nested loop and one with a helping function call for printing contiguous alphabets.