In this Python program, you will learn to print the right angled triangle pattern of zigzag numbers using for loop, while loop, and functions.
The below example accepts the user-entered rows, and we used multiple nested for loops to iterate the rows and columns. Next, the program will print the right angled triangle of zigzag numbers.
r = int(input("Enter Rows = "))
n = 1
for i in range(1, r + 1):
if i % 2 != 0:
for j in range(1, i + 1):
print(n, end = ' ')
n += 1
else:
m = n + i - 1
for j in range(i):
print(m, end = ' ')
m -= 1
n += 1
print()
Enter Rows = 12
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28
36 35 34 33 32 31 30 29
37 38 39 40 41 42 43 44 45
55 54 53 52 51 50 49 48 47 46
56 57 58 59 60 61 62 63 64 65 66
78 77 76 75 74 73 72 71 70 69 68 67
Instead of a For loop, this program uses the while loop to iterate the right angled triangle pattern rows and columns and prints the zigzag numbers in each position. For more Number pattern programs >> Click Here.
r = int(input("Enter Rows = "))
n = 1
i = 1
while i <= r:
if i % 2 != 0:
j = 1
while j <= i:
print(n, end = ' ')
j += 1
n += 1
else:
m = n + i - 1
j = 0
while j < i:
print(m, end = ' ')
m -= 1
j += 1
n += 1
i += 1
print()
Enter Rows = 13
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28
36 35 34 33 32 31 30 29
37 38 39 40 41 42 43 44 45
55 54 53 52 51 50 49 48 47 46
56 57 58 59 60 61 62 63 64 65 66
78 77 76 75 74 73 72 71 70 69 68 67
79 80 81 82 83 84 85 86 87 88 89 90 91
In this Python program, we have created a ZigzagNumbersRightTriangle function that accepts rows as the parameter value. Next, it uses the nested for loops and if else statement to print the right angled triangle of zigzag numbers on each row.
def ZigzagNumbersRightTriangle(rows):
n = 1
for i in range(1, rows + 1):
if i % 2 != 0:
for j in range(1, i + 1):
print(n, end=' ')
n += 1
else:
m = n + i - 1
for j in range(i):
print(m, end=' ')
m -= 1
n += 1
print()
n = int(input("Enter Rows = "))
ZigzagNumbersRightTriangle(n)