Shapes Printing Challenge: Numbers/Alphabets Triangles
Explore how to use nested for loops to create various triangle shapes with numbers and alphabets. This lesson helps you master printing upright, upside-down, incremental, and palindromic triangles, enhancing your understanding of iteration and pattern logic in programming.
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!
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!
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!
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!
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
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