Advertisements
Advertisements
Question
Write a program to print the following pattern:
| 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 |
Short/Brief Note
Advertisements
Solution
n = 5
for i in range (1, n + 1):
#This will print the space before the number
space = (n - i) * " "
print(space, end = " ")
#This for loop will print the decreasing numbers
for k in range(i, 1, -1):
print(k, end = " ")
#This for loop will print the increasing numbers
for j in range(1, i + 1):
print(j, end = " ")
#Print a new line after the space and numbers are printed
print()shaalaa.com
Is there an error in this question or solution?
Chapter 6: Flow of Control - Exercise [Page 141]
