Shapes Printing Challenge: Numbers/Alphabets Triangles
Test your problem-solving skills in this challenging exercise.
We'll cover the following
Use nested for
loops for the upcoming tasks.
Printing triangle with numbers
In this section, we have a few coding challenges for you.
Challenge 1: Upright right triangle
Your code should print the following triangles. Consider h
could be any of the values between 1
to 10
.
Note: Make sure to write code depending on a specific value of h
.
Sample output
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
Good luck!
#include <iostream>using namespace std;void printT1(int h){// write your code here}
Challenge 2: Upside down right triangle
Your code should print the following triangles, consider h
could be any of the values between 1
to 10
.
Note: y> Make sure to write code depending on a specific value of h
.
Sample output
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
Good luck!
#include <iostream>using namespace std;void printT2(int h){//write your code here}
Challenge 3: Upright triangle (with incremental values)
Your code should print the following triangles, consider h
could be any of the values between 1
to 10
.
Note: Make sure to write code depending on a specific value of h
.
Sample output
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
Good luck!
#include <iostream>using namespace std;void printT3(int h){// write your code here}
Challenge 4: Palindromic triangle of numbers
Your code should print the following triangles, considering h
could be any of the values between 1
to 10
.
Note: Make sure to write code depending on a specific value of h
.
Sample output
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
Good luck!
#include <iostream>using namespace std;void printT4(int h){// Write your code here}
Printing triangle with alphabets
Let us play with triangles of the alphabets now.
Challenge 5: Upright triangle of letters 1
Your code should print the following triangles, considering h
could be any of the values between 1
to 10
.
Note: Make sure to write code depending on a specific value of h
.
Sample output
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
#include <iostream>using namespace std;void printT5(int h){//write your code here}
Printing triangle with alphabets
Challenge 6: Upright right triangle of letters 2
Your code should print the following triangles, considering h
could be any of the values between 1
to 10
.
Note: Make sure to write code depending on a specific value of h
.
Sample output
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
#include <iostream>using namespace std;void printT6(int h){// Write your code}