In this program, we will print number patterns.
Steps to solve the program
We will divide this problem into 2 part
Part 1: First will print a triangle of numbers in increasing order.
1. set variable num1 = 1
2. Initiate a for loop “for i in range(5)”
3. Initiate a nested for loop “for i in range(i+1)”
the nested loop will execute with the initiate value of the outer loop
4. print variable num1, which we initiated in step 1, and increase
its value by 1 with each iteration of the nested loop.
5. print(), will change the line after each iteration of the outer loop.
Part 2: Second will print a triangle of numbers in decreasing order.
1. Initiate a for loop with a start value of 5, an end value of 0 decreases it by -1.
“for k in range(5, 0, -1)”
2. Initiate a nested for loop with start value 0 and end value k-1
“for l in range(k+1)”
the nested loop will execute with the initial value of the outer loop
3. print variable num1, which we initiated, and decrease its value by 1 with each iteration of the nested loop.
4. print(), will change the line after each iteration of the outer loop.
# Part1 : First will print triangle of numbers in increasing order.
num1 = 1
for i in range(5):
for j in range(i+1):
print(num1, end=" ")
num1 += 1
print()
# Part2 : Second will print triangle of number is decreasing order.
for k in range(5, 0, -1):
for l in range(k-1):
num1 -= 1
print(num1, end=" ")
print()
Output :
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
15 14 13 12
11 10 9
8 7
6