In this code, we are going to see how to print the 12345 1234 123 12 1 pattern in Python using a for loop.
Pattern programs are really good for building logic.
The logic of the code is:
- The inner loop moves horizontally, whereas the outer loop moves vertically.
- The outer loop will iterate from i = n to i = 0.
- The inner loop will iterate from j = 0 to j = i + 1.
- The inner loop will print the value of j + 1 in every iteration.
- After the complete iteration of the inner loop, the cursor will move to next line.
n = int(input("Enter number of rows: "))
for i in range(n,0,-1):
for j in range(0, i):
print(j + 1, end = "")
print("\n")
The output of the code is
Enter number of rows: 6
123456
12345
1234
123
12
1
Enter number of rows: 5
12345
1234
123
12
1